首页 > 解决方案 > URL 错误 404 找不到 Django 页面错误 x.html

问题描述

我还没有设法找到为什么它会给出错误?在 StackOverflow 中复制粘贴以获得 exp 的地方收到了很多答案

所以情况是我的 navbar.html 中的 URL 返回错误 404 找不到 x.html

这是每个 py 文件的代码:

视图.py:

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

设置.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '../blog_level/static')
STATICFILES_DIRS= [
    os.path.join(BASE_DIR, 'static'),
]

导航栏.html:

 <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="{% url 'homepage' %}" 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="{% url 'education' %}" 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="{% url 'portfolio' %}" 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="{% url 'homepage' %}#sec-1120" data-page-id="134427998" style="padding: 10px 0px; text-shadow: 0 0 8px rgba(128,128,128,1);">Contact</a>
</li></ul>

blog_level\urls.py :

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

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

first_website_app_level.py :

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

urlpatterns = [
    path('', include('blog_level.urls')),


]

下面是我的项目的文件结构文件结构

并请找到调试错误:

调试错误 404 页面

我仍然不明白为什么它不会链接到正确的页面的过程的错误,自然基于以下专家在一个简单的项目级别。

https://github.com/CoreyMSchafer/code_snippets/blob/master/Django_Blog/03-Templates/django_project/blog/urls.py

标签: pythonhtmldjango-viewsdjango-templates

解决方案


In是在andDjango urls.py之间具有映射的文件,因此如果您想访问页面,您必须拥有您的as (代码中没有任何更改,只需更新您在浏览器中使用的 URL),因为映射为viewsurlExperience.htmlurl127.0.0.1:8000/experienceurls.pypath('experience/', views.experience, name='experience'),

是一个关于 url 映射如何工作的好文档。


推荐阅读