Django:';网络学习&x27;不是已注册的命名空间
我是Django的初学者,到目前为止,我一直在学习本教程,除了给应用程序命名为Django:';网络学习&x27;不是已注册的命名空间,django,Django,我是Django的初学者,到目前为止,我一直在学习本教程,除了给应用程序命名为weblearn,而不是像本教程中那样命名为polls。到目前为止,一切正常 我在第四页的一半左右,刚刚创建了文件weblearn/templates/weblearn/results.html,我再次检查了名称polls是否未在任何代码/html中使用 但是,当我导航到页面http://127.0.0.1:8000/weblearn/1/根据建议,我收到一个错误 In template /Users/alex/Cod
weblearn
,而不是像本教程中那样命名为polls
。到目前为止,一切正常
我在第四页的一半左右,刚刚创建了文件weblearn/templates/weblearn/results.html
,我再次检查了名称polls
是否未在任何代码/html中使用
但是,当我导航到页面http://127.0.0.1:8000/weblearn/1/
根据建议,我收到一个错误
In template /Users/alex/Coding/Django/learnit/weblearn/templates/weblearn/detail.html, error at line 5
'weblearn' is not a registered namespace
如何调试此错误?我一点也不知道这个错误意味着什么
detail.html
:
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'weblearn:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
<input type="submit" value="Vote">
</form>
向您的URL.py添加
app\u name
from django.urls import path
from . import views
app_name = 'weblearn'
# your urlpatterns
通过这种方式,
{%url'weblearn:vote'question.id%}
将知道它必须查看weblearn/url.py
似乎您正在使用名称空间生成操作url,{%url'weblearn:vote'question.id%}
,weblearn
是名称空间,vote
是url名称。要使其工作,您应该在ROOT_URLCONF(在settings.py中)中定义名称空间,如下所示:
project/settings.py(根目录)
你能分享你的
url.py
内容吗?可以是这样的,允许您从weblearn
namespace,path(“”,include('weblearn.url',namespace='weblearn'))获取url。请参阅更新的问题谢谢,这很有效。我错过了教程中的内容吗?是的,没错,上面是urlpatterns
。这件事在我身上发生过好几次。
from django.urls import path
from . import views
app_name = 'weblearn'
# your urlpatterns
from django.urls import include
app_name = 'weblearn'
urlpatterns = [
path('weblearn/', include('weblearn.urls', namespace='weblearn')),
]