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 仍然让我摸不着头脑。
它现在在报告应用程序中,因此您需要前缀:
{% url "reporting:line_chart_json" %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句