Django - Python - URL与这些都不匹配



当我在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')

这是我在导航栏中点击两次about时的错误,当我在urls.py:中有带about/的.html后缀时

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.

这是我在导航栏中点击about时的错误,当我在urls.py:中没有带about/的.html后缀时

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 -->

我似乎解决不了这个问题,请帮忙。

原因是在html中,你可能没有在导航之前放斜线。它应该像这个

<a href=“/about/”>

这将转到about.html,而不是在url的末尾添加/about.html。

所以在导航栏上应该是:

<li class="nav-item"><a href="/about/" class="nav-link">About</a></li>

将导航栏更改为:

<div class="collapse navbar-collapse" id="ftco-nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"><a href="/index/" class="nav-link">Home</a></li>
<li class="nav-item"><a href="/about/" class="nav-link">About</a></li>
<li class="nav-item"><a href="/categories/" class="nav-link">Categories</a></li>
<li class="nav-item"><a href="/docAdd/" class="nav-link">Add New Documents</a></li>
<li class="nav-item cta"><a href="/contact/" class="nav-link"><span>Get in touch</span></a></li>
</ul>
</div>
</div>

urls.py应该看起来像:

from django.contrib import admin
from django.urls import path
from main import views
urlpatterns = [
path('admin/', admin.site.urls),
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')
]

我希望这能帮助你,如果你不理解我所说的,请发表评论。

首先,您不应该在url文件中包含.html

当你把链接放在<a>标签中时,你应该使用一个djangourl reversion模板标签,如下所示:

{% url 'about' %}
<li class="nav-item"><a href="{% url 'about' %}">About</a></li>

还要确保你的应用程序目录中有一个模板文件设置。

最新更新