【发布时间】:2021-07-14 03:22:33
【问题描述】:
我正在学习 django,我不太明白这是 django 的行为还是我做错了什么。 所以基本上我尝试使用 django 模板标签来创建一个固定的 html 页眉和页脚以从它扩展 base.html 是这样的
<a href="{% url "index" %}" style="text-decoration:none;"><h1 style="text-align: center;color: green">Welcome to HireMe</h1></a>
{% block title %}
{% endblock title %}
{% block content %}
{% endblock content %}
<a href="{% url "about" %}" style="text-decoration:none;"><h4 style="text-align: left;color: purple;margin-top:256px;">About us</h4></a>
而index.html是这样的
{% extends 'base.html' %}
{% block content %}
<body style="background-color: rgb(243,220,245) ">
<div align="center">
<p>Fugiat commodo officia laborum esse magna nisi commodo eu est non sunt in ut adipisicing nulla cupidatat dolor.</p>
<img src="https://media4.giphy.com/media/3pZipqyo1sqHDfJGtz/giphy.gif">
</div>
</body>
{% endblock content%}
当我在这里使用 url 模板标签 href="{% url "index" %}" 它应该带我到索引 根据我在 url 模式中命名的页面
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', include('pages.urls'), name='test'),
path('about', include('pages.urls'), name='about'),
path('', include('pages.urls'), name='index'),
]
只要 "" 模式是最后一个,但如果我将这些行更改为这个
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', include('pages.urls'), name='test'),
path('', include('pages.urls'), name='index'),
path('about', include('pages.urls'), name='about'),
]
它带我到 /about 页面,如果我将 href="{% url "index" %}" 更改为 href="{% url "about" %}" 它带我到 /aboutabout 页面,那么url 值始终是最后一个模式,否则我做错了什么
pages/url.py 文件
from django.urls import path
from .views import HomePageView, AboutPageView, TestPageView
urlpatterns = [
path('about', AboutPageView.as_view(), name='about'),
path('test', TestPageView.as_view(), name='test'),
path('', HomePageView.as_view(), name='index'),
]
查看文件
from django.shortcuts import render
from django.views.generic import TemplateView
from django.http import HttpResponse
# Create your views here.
class HomePageView(TemplateView):
template_name = 'index.html'
class AboutPageView(TemplateView):
template_name = 'about.html'
class TestPageView(TemplateView):
template_name = 'test.html'
【问题讨论】:
标签: python-3.x django django-rest-framework django-templates