【发布时间】: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' %} 有效吗? 我能做什么以及最简单的选择?
【问题讨论】: