【发布时间】:2020-08-02 17:02:18
【问题描述】:
当我写这样的代码时,在about/ 的末尾没有.html:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
path('categories/', views.categories, name='categories'),
path('docAdd/', views.docAdd, name='docAdd')
]
在我的网页导航栏中单击 about 时出现错误,这种情况下的错误是:
" 当前路径 about.html 与其中任何一个都不匹配。"但是,当我将 about/ 更改为 about.html/ 时,我可以在导航栏中单击 about 并打开页面,但是当我在导航栏中从 about 切换到 contact 时,出现如下错误:“当前路径,about.html/contact.html ,与这些都不匹配。”
这是带有.html后缀的代码:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about.html/', views.about, name='about'),
path('contact.html/', views.contact, name='contact'),
path('categories.html/', views.categories, name='categories'),
path('docAdd.html/', views.docAdd, name='docAdd')
]
views.py 代码:
from django.shortcuts import render
from . models import Document
def index(request):
return render(request, 'index.html')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
def categories(request):
return render(request, 'categories.html')
def docAdd(request):
return render(request, 'docAdd.html')
这是我在导航栏中单击两次时的错误,当我在 urls.py 中有 .htmlsuffix 和 about/ 时:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/about.html/about.html
Using the URLconf defined in thesisProject.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
about.html/ [name='about']
contact.html/ [name='contact']
categories.html/ [name='categories']
docAdd.html/ [name='docAdd']
^media/(?P<path>.*)$
The current path, about.html/about.html, didn't match any of these.
这是我在导航栏中单击一次时的错误,当我在 urls.py 中没有带有 about/ 的 .htmlsuffix 时:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/about.html/about.html
Using the URLconf defined in thesisProject.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
about.html/ [name='about']
contact.html/ [name='contact']
categories.html/ [name='categories']
docAdd.html/ [name='docAdd']
^media/(?P<path>.*)$
The current path, about.html, didn't match any of these.
index.html 中导航栏的代码:
<div class="collapse navbar-collapse" id="ftco-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
<li class="nav-item"><a href="about.html" class="nav-link">About</a></li>
<li class="nav-item"><a href="categories.html" class="nav-link">Categories</a></li>
<li class="nav-item"><a href="docAdd.html" class="nav-link">Add New Documents</a></li>
<li class="nav-item cta"><a href="contact.html" class="nav-link"><span>Get in touch</span></a></li>
</ul>
</div>
</div>
</nav>
<!-- END nav -->
我似乎无法解决这个问题,请帮忙。
【问题讨论】:
-
你能展示用于创建导航栏的模板代码吗?
-
@FiddleStix 我也添加了,请检查
标签: python django django-views django-urls