【问题标题】:Reverse for 'X' not found. 'X' is not a valid view function or pattern name. Django Framework未找到“X”的反向。 “X”不是有效的视图函数或模式名称。 Django 框架
【发布时间】:2021-10-23 01:23:19
【问题描述】:

我是 Django 概念的新手

我有 3 个文件可以解决这个问题。

我的项目文件名为 firt_website 中有一个 urls.py,代码如下:

"""firt_website URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from pages import urls

app_name = 'main'

urlpatterns = [
    path('', include('pages.urls', namespace='home')),
    path('portfolio/', include('pages.urls', namespace='portfolio')),
    path('experience/', include('pages.urls', namespace='experience')),
    path('education/', include('pages.urls', namespace='education')),

]

我有导航栏 HTML,它将作为 {% extends 'navbar.html' %} 包含在索引页面 HTML 中:文件包含:

 <div class="u-custom-menu u-nav-container">
            <ul class="u-custom-font u-font-titillium-web u-nav u-spacing-30 u-unstyled u-nav-1"><li class="u-nav-item"><a class="u-border-2 u-border-active-white u-border-hover-custom-color-2 u-border-no-left u-border-no-right u-border-no-top u-button-style u-nav-link u-text-active-white u-text-hover-custom-color-2 u-text-white" href="Acceuil.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Home</a>
</li><li class="u-nav-item"><a class="u-border-2 u-border-active-white u-border-hover-custom-color-2 u-border-no-left u-border-no-right u-border-no-top u-button-style u-nav-link u-text-active-white u-text-hover-custom-color-2 u-text-white" href="Experience.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Experience</a>
</li><li class="u-nav-item"><a class="u-border-2 u-border-active-white u-border-hover-custom-color-2 u-border-no-left u-border-no-right u-border-no-top u-button-style u-nav-link u-text-active-white u-text-hover-custom-color-2 u-text-white" href="Education.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Study</a>
</li><li class="u-nav-item"><a class="u-border-2 u-border-active-white u-border-hover-custom-color-2 u-border-no-left u-border-no-right u-border-no-top u-button-style u-nav-link u-text-active-white u-text-hover-custom-color-2 u-text-white" href="Portfolio.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Portfolio</a>
</li><li class="u-nav-item"><a class="u-border-2 u-border-active-white u-border-hover-custom-color-2 u-border-no-left u-border-no-right u-border-no-top u-button-style u-nav-link u-text-active-white u-text-hover-custom-color-2 u-text-white" href="Acceuil.html#sec-1120" data-page-id="134427998" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Contact</a>
</li></ul>
          </div>
          <div class="u-custom-menu u-nav-container-collapse">
            <div class="u-align-center u-black u-container-style u-inner-container-layout u-opacity u-opacity-95 u-sidenav">
              <div class="u-sidenav-overflow">
                <div class="u-menu-close"></div>
                <ul class="u-align-center u-nav u-popupmenu-items u-unstyled u-nav-2"><li class="u-nav-item"><a class="u-button-style u-nav-link" href="Acceuil.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Home</a>
</li><li class="u-nav-item"><a class="u-button-style u-nav-link" href="{% url 'experience' %}" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Experience</a>
</li><li class="u-nav-item"><a class="u-button-style u-nav-link" href="Education.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Study</a>
</li><li class="u-nav-item"><a class="u-button-style u-nav-link" href="Portfolio.html" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Portfolio</a>
</li><li class="u-nav-item"><a class="u-button-style u-nav-link" href="Acceuil.html#sec-1120" data-page-id="134427998" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Contact</a>
</li></ul>
              </div>
            </div>

然后是 pages/urls.py 文件夹 project_app 称为 pages :

from django.contrib import admin
from django.urls import path
from . import views

app_name = 'pages'
urlpatterns = [
    path('', views.home_page, name='homepage'),
    path('education/', views.education, name='education'),
    path('experience/', views.experience, name='experience'),
    path('portfolio/', views.portfolio, name='portfolio'),
]

这是我的视图文件,其中包含指向 HTML 文档的链接

rom django.shortcuts import render, redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from .forms import ContactForm

# Create your views here.


def home_page(request):
    return render(request, "index.html")


def education(request):
    return render(request, 'Education.html')


def experience(request):
    return render(request, 'Experience.html')


def portfolio(request):
    return render(request, 'Portfolio.html')

我一直在努力创建一个导航栏,无论我是在索引页面上,还是想进入投资组合页面,然后在进入投资组合页面后返回索引页面

我尝试过所有类型的 Django 代码和版本,但没有多少展示如何做导航栏...

你知道为什么执行 {% url 'experience' %} 有效吗? 我能做什么以及最简单的选择?

error Django reverse match

【问题讨论】:

    标签: python html css django


    【解决方案1】:

    由于您指定了app_name = 'main',这意味着您需要在视图名称前加上app_name,所以:

    {% url '<strong>main:</strong>experience' %}

    【讨论】:

    • 非常感谢,它可以工作,但是当我现在点击它时,它显示以下错误:找不到页面(404)请求方法:GET 请求 URL:127.0.0.1:8000/Experience.html 使用定义的 URLconf firt_website.urls,Django 尝试了这些 URL 模式,顺序如下: [name='homepage'] education/ [name='education'] experience/ [name='experience'] 投资组合/ [name='portfolio'] 投资组合/ experience/education/ 当前路径 Experience.html 与其中任何一个都不匹配。
    • @yedx_1_10:那是因为你写了hrefs,就像href="Experience.html"。但是 URL 只是 experience/,所以 没有 .html,您也应该将其替换为 href="{% url 'main:experience %}",以便在您更改 url 模式时轻松调整。
    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 2019-04-06
    • 2021-04-21
    • 2018-01-25
    • 2019-06-27
    • 2018-11-19
    • 2019-11-01
    • 2021-06-11
    相关资源
    最近更新 更多