[英]Im getting a 'NoReverseMatch' error during template rendering
Currently reading the Python Crash Course by Eric Matthes and I'm trying to code a simple learning lo using python and Django, but I keep getting an error when I try to enter the topics URL.目前正在阅读 Eric Matthes 的 Python Crash Course,我正在尝试使用 python 和 Django 编写一个简单的学习 lo,但是当我尝试输入主题 URL 时,我一直收到错误消息。 The error seems to come from the base.html(the template), it says that learning_logs is not a registered namespace, even tho I have written it as a variable named app_name and have included it in the URLs.错误似乎来自 base.html(模板),它说 learning_logs 不是注册的命名空间,即使我已经将其编写为名为 app_name 的变量并将其包含在 URL 中。
Here's the code:这是代码:
main urls.py:主要网址.py:
from django.contrib import admin
from django.urls import path , include , re_path
app_name = 'learning_logs'
urlpatterns = [
path('admin/', admin.site.urls),
path('' , include(("learning_logs.urls", app_name ) ))
]
urls.py:网址.py:
from django.urls import path , re_path
from . import views
app_name = 'learning_logs'
urlpatterns = [
#Home page
path('', views.index, name='index'),
path('topics/', views.topics , name='topics'),
re_path(r'^topics/(?P<topic_id>\d+)/$' , views.topic , name = 'topic'),
re_path(r'^new_topic/$' , views.new_topic , name = 'new_topic'),
re_path(r'^new_entry/(?P<topic_id>\d+)/$' , views.new_entry , name = 'new_entry'),
]
base.html:基地.html:
<p>
<a href="{% url 'learning_logs:index' %}">Learning Log</a>
<a href="{% url 'learning_logs:topics' %}">Topics</a>
</p>
{% block content %}
{% endblock content %}
Error:错误:
I think you need to fix the declarations in the main urls.py by removing the app_name
and fixing the include.我认为您需要通过删除app_name
并修复包含来修复主urls.py 中的声明。
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('' , include('learning_logs.urls'))
]
Just fix the main urls.py
file as the following:只需将主urls.py
文件修复如下:
from django.contrib import admin
from django.urls import path , include , re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('' , include(("learning_logs.urls", app_name='learning_logs') ))
]
Note the named parameter app_name=
in include
注意include
的命名参数app_name=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.