无法移动URL来导入Django中的URL

问题描述 投票:0回答:1

django 2.2.5

到目前为止,我一直没有遇到过移动网址的问题。

reporting / reporting.html(索引):

...
{% url "line_chart_json" %}
...

报告/ views.py

class LineChartJSONView(BaseLineChartView):
    def get_labels(self):
        """Return 7 labels for the x-axis."""
        return ["January", "February", "March", "April", "May", "June", "July"]
    ....

在主应用程序url.py中时,就可以了

from django.urls import path, include
from reporting.views import LineChartJSONView
urlpatterns = [
    ...
    path('reporting/', include('reporting.urls')),
    path('line_chart/json/', LineChartJSONView.as_view(),
          name='line_chart_json'),
]

当我将其移至报告时

from django.urls import path
from . import views
from .views import LineChartJSONView

app_name = 'reporting'
urlpatterns = [
    path('', views.summary_properties_user,
         name='index'),
    path('line_chart/json/', LineChartJSONView.as_view(),
         name='line_chart_json'),
]

[reporting.html出现错误,我回来了:

NoReverseMatch at /reporting/
Reverse for 'line_chart_json' not found. 'line_chart_json' is not a valid view function or pattern name.

我假设一个简单的疏忽。仅仅过了几周,Django URL仍然可以吸引我。

python django django-templates django-views django-urls
1个回答
0
投票

现在在报告应用程序中,因此您需要前缀:

{% url "reporting:line_chart_json" %}
© www.soinside.com 2019 - 2024. All rights reserved.