【发布时间】:2021-09-01 00:19:19
【问题描述】:
我查看了官方的 Django 文档(https://docs.djangoproject.com/en/3.2/topics/http/urls/#reversing-namespaced-urls)和错误代码,并写了以下内容。如何编写命名空间来工作?
views.py:
from django.urls import path, include
app_name = 'home'
urlpatterns = [
path('math/', include('math_note.urls', namespace='math-note'))
]
模板:
<a href="{% url 'home:math-note' %}">math note</a>
运行运行服务器时出错:
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
问题: 如何使用命名空间功能?
第二个问题:
谢谢!连接良好
<a href="{% url 'music:home-page' %}">(to app_name = 音乐)
但是,当我从音乐应用程序调用表单并加载要生成的 url 时,出现错误。其他 URL 没有任何问题,只有这个。
Reverse for 'home-page' not found. 'home-page' is not a valid view function or pattern name.
#music/urls.py
from django.urls import path
from . import views
app_name = 'music'
urlpatterns = [
path('', views.music_home_page, name='home-page'),
path('new/', views.new_song, name='new'),
path('music_player/<int:id>/', views.music_player, name='music-player'),
]
问题: 我解决了前面的问题,得到了下面的问题。这是什么原因和解决方法?
【问题讨论】:
-
你需要在
math_note.urls中指定一个app_name。
标签: python django django-urls