我的 pages/templates/base.html 中用于標題模板的鏈接會導致 404 錯誤。手動撰寫“http://127.0.0.1:8000”“http://127.0.0.1:8000/about/”時,頁面加載正確。我正在使用基于類的視圖并遵循 Django 的第 3 章為初學者(威廉文森特)。
頁面/模板/base.html:
<header>
<a href="{% url 'home' %">Home</a> |
<a href="{% url 'about' %">About</a>
</header>
頁面/urls.py:
from django.urls import path
from .views import HomePageView, AboutPageView
urlpatterns = [
path("", HomePageView.as_view(), name="home"),
path("about/", AboutPageView.as_view(), name="about"),
]
投資組合/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("pages.urls")),
]
設定.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages.apps.PagesConfig',
]
頁面/views.py:
from django.views.generic import TemplateView
# Create your views here.
from django.http import HttpResponse
class HomePageView(TemplateView):
template_name = 'home.html'
class AboutPageView(TemplateView):
template_name = "about.html"
錯誤如下:
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
about/ [name='about']
The current path, about/{% url 'about' %, didn’t match any of these.
終端錯誤:
Not Found: /{% url 'about' %
[26/Oct/2022 11:40:02] "GET /{% url 'about' % HTTP/1.1" 404 2497
我認為問題在于pages/urls.py,好像從'http://127.0.0.1:8000/about/'點擊'home'鏈接,url是'http://127.0.0.1:8000/about /{% url 'home' %'。我試圖從“”中洗掉“about/ path("about/", AboutPageView.as_view(), name="about"),
”
我也不確定href標簽是否寫得正確。我已經看過這個和這個,但無法弄清楚。謝謝。
uj5u.com熱心網友回復:
{% url 'about' %<-您需要正確關閉它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520089.html
