Django-Python-URL与以上任何都不匹配

问题描述 投票:0回答:1

[当我在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.html与任何这些都不匹配。”但是,当我将about /更改为about.html /时,我可以在导航栏中单击About并打开页面,但是当我从导航栏中的要切换为联系时,我会收到类似以下错误:“当前路径,about.html / contact.html ,没有任何一个与之匹配。”我似乎无法解决此问题,请帮助。

这是带有about.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

# Create your views here.


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

这是我在导航栏中两次单击关于时的错误:

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.

index.html中导航栏的代码:

  <nav class="navbar navbar-expand-lg navbar-dark ftco_navbar bg-dark ftco-navbar-light" id="ftco-navbar">
    <div class="container">
      <a class="navbar-brand" href="index.html">Ugalizer</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#ftco-nav"
        aria-controls="ftco-nav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="oi oi-menu"></span> Menu
      </button>

      <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 -->
python django django-views django-urls
1个回答
0
投票

原因是,在html中,您可能没有在导航之前添加斜线。应该是这样的

 <a href=“/about.html/”>

这将转到about.html,而不是在URL末尾添加/about.html。希望这对您有所帮助,如果您不理解我的意见,请发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.