[英]Im getting a 'NoReverseMatch' error during template rendering
目前正在閱讀 Eric Matthes 的 Python Crash Course,我正在嘗試使用 python 和 Django 編寫一個簡單的學習 lo,但是當我嘗試輸入主題 URL 時,我一直收到錯誤消息。 錯誤似乎來自 base.html(模板),它說 learning_logs 不是注冊的命名空間,即使我已經將其編寫為名為 app_name 的變量並將其包含在 URL 中。
這是代碼:
主要網址.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 ) ))
]
網址.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'),
]
基地.html:
<p>
<a href="{% url 'learning_logs:index' %}">Learning Log</a>
<a href="{% url 'learning_logs:topics' %}">Topics</a>
</p>
{% block content %}
{% endblock content %}
錯誤:
我認為您需要通過刪除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'))
]
只需將主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') ))
]
注意include
的命名參數app_name=
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.