无法移动 URL 以在 Django 中导入 URL

django 2.2.5


到目前为止,我在移动 url 时没有遇到问题。


报告/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 URLs 仍然让我印象深刻。


PIPIONE
浏览 83回答 1
1回答

长风秋雁

它现在在报告应用程序中,因此您需要前缀:{% url "reporting:line_chart_json" %}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python