Python 如何像使用常规模板一样使用base.html并将其呈现
我在base.html中有导航栏css,然后使用{%extend base.html%}将其添加到常规模板中。但是,我希望在我的导航栏(位于my base.html中)中有一个login/register选项,并且我希望使用cookies来决定是将login还是register放在导航栏中。当base.html没有视图时,如何让它能够请求cookies?另外,我的站点上并不是每个网页都有一个视图,有些是带有{%extend base.html%}的普通html my base.html:Python 如何像使用常规模板一样使用base.html并将其呈现,python,django,Python,Django,我在base.html中有导航栏css,然后使用{%extend base.html%}将其添加到常规模板中。但是,我希望在我的导航栏(位于my base.html中)中有一个login/register选项,并且我希望使用cookies来决定是将login还是register放在导航栏中。当base.html没有视图时,如何让它能够请求cookies?另外,我的站点上并不是每个网页都有一个视图,有些是带有{%extend base.html%}的普通html my base.html: <
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-175481126-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-175481126-1');
</script>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
font-family: Tahoma, Geneva, sans-serif;
background-color: #333;
}
li {
float: left;
border-right: 1px solid #bbb;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
color: white;
text-decoration:none;
}
/* for buttons*/
.paging {
background-color: #333;
border: none;
color: white;
padding: 8px 14px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
font-family: Tahoma, Geneva, sans-serif;
}
button:hover {
background-color: #111;
}
.register {
float: right;
}
.paging {
background-color: #333;
border: none;
color: white;
padding: 8px 14px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
font-family: Tahoma, Geneva, sans-serif;
}
button:hover {
background-color: #111;
}
/* title */
.title {
color: black;
padding: 10px;
text-align: center;
font-size: 40px;
font-family: Tahoma, Geneva, sans-serif;
Color: black;
text-decoration: none;
}
/* descriptions */
.description {
color: black;
padding: 15px;
font-family: Tahoma, Geneva, sans-serif;
text-align: left;
font-size: 13px;
}
/* embedd url */
.iframe-container{
position: relative;
width: 100%;
padding-bottom: 56.25%;
height: 0;
margin-left: auto; /* Automatic margin from left */
margin-right: auto; /* Automatic margin from right */
}
.iframe-container iframe{
position: absolute;
top:0;
left: 0;
width: 100%;
height: 100%;
}
/* make columns */
* {
box-sizing: border-box;
}
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
flex: 0 0 100%;
max-width: 100%;
/* you can just remove these margins all together. Just wanted to emphasize that there should be no margin left or right, to avoid overflow */
margin-left: 0px;
margin-right: 0px;
}
.videos, .ads {
flex-grow: 0; flex-shrink: 0;
}
.ads {
display: block;
float: right;
background-color: #c1c4c9;
width: 25%;
}
.videos {
display: block;
float: left;
width: 75%;
padding-right: 10%;
padding-left: 10%;
}
.image {
padding-right: 5%;
padding-left: 5%;
padding-top: 5%;
padding-bottom: 5%;
}
.logo {
flex-direction:; row;
flex:; 0 0 100%;
max-width 100%;
justify-content:; center;
}
.banner {
/* float: left;*/
width: 100%;
height: 5%;
background-color: red;
color: white;
text-align: center;
}
img {
max-width: 100%;
max-height: 100%;
}
/* header */
.header {
background-color: #F1F1F1;
text-align: center;
padding: 20px;
font-family: Tahoma, Geneva, sans-serif;
}
</style>
<html>
{% load static %}
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}"/>
<div class="logo">
<div class="header">
<a href="/">
<img src="https://i.ibb.co/5Rqps7F/logo-black.png" alt="Computer Man">
</a>
</div>
<ul>
<li class="all"><a href="/">All videos</a></li>
<li class="stam"><a href="/stam">Stam videos</a></li>
<li class="music"><a href="/music">Music videos</a></li>
<li class="news"><a href="/news">News videos</a></li>
<li class="contact"><a href="/contact">Contact</a></li>
<li class="register"><a href="/register">Login/Register</a></li>
</ul>
<div class="banner">
<text align="center">We are still developing our site, so we may not be able to update videos as often as we want</text>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
window.dataLayer=window.dataLayer | |[];
函数gtag(){dataLayer.push(参数);}
gtag('js',新日期());
gtag(“配置”、“UA-175481126-1”);
保险商实验室{
列表样式类型:无;
保证金:0;
填充:0;
溢出:隐藏;
字体系列:塔荷马,日内瓦,无衬线;
背景色:#333;
}
李{
浮动:左;
右边框:1px实心#bbb;
}
李阿{
显示:块;
颜色:白色;
文本对齐:居中;
填充:14px 16px;
文字装饰:无;
}
/*悬停时将链接颜色更改为#111(黑色)*/
李娜:停下来{
背景色:#111;
颜色:白色;
文字装饰:无;
}
/*按钮*/
.传呼{
背景色:#333;
边界:无;
颜色:白色;
填充:8px 14px;
文本对齐:居中;
文字装饰:无;
显示:内联块;
字体大小:16px;
字体系列:塔荷马,日内瓦,无衬线;
}
按钮:悬停{
背景色:#111;
}
.登记{
浮动:对;
}
.传呼{
背景色:#333;
边界:无;
颜色:白色;
填充:8px 14px;
文本对齐:居中;
文字装饰:无;
显示:内联块;
字体大小:16px;
字体系列:塔荷马,日内瓦,无衬线;
}
按钮:悬停{
背景色:#111;
}
/*头衔*/
.头衔{
颜色:黑色;
填充:10px;
文本对齐:居中;
字体大小:40px;
字体系列:塔荷马,日内瓦,无衬线;
颜色:黑色;
文字装饰:无;
}
/*描述*/
.说明{
颜色:黑色;
填充:15px;
字体系列:塔荷马,日内瓦,无衬线;
文本对齐:左对齐;
字体大小:13px;
}
/*嵌入url*/
.iframe容器{
位置:相对位置;
宽度:100%;
垫底:56.25%;
身高:0;
左边距:自动;/*自动左边距*/
右边距:自动;/*自动右边距*/
}
.iframe容器iframe{
位置:绝对位置;
排名:0;
左:0;
宽度:100%;
身高:100%;
}
/*列*/
* {
框大小:边框框;
}
.行{
显示器:flex;
弯曲方向:行;
柔性包装:包装;
弹性:0.100%;
最大宽度:100%;
/*您可以将这些边距一起删除。我只是想强调,应该没有左边或右边的边距,以避免溢出*/
左边距:0px;
右边距:0px;
}
.视频,.广告{
弹性增长:0;弹性收缩:0;
}
.广告{
显示:块;
浮动:对;
背景色:#c1c4c9;
宽度:25%;
}
.视频{
显示:块;
浮动:左;
宽度:75%;
右:10%;
左:10%;
}
.形象{
右:5%;
左:5%;
垫面:5%;
垫底:5%;
}
.标志{
弯曲方向:;行;
弹性:0.100%;
最大宽度100%;
证明内容:;中心;
}
.横幅{
/*浮动:左*/
宽度:100%;
身高:5%;
背景色:红色;
颜色:白色;
文本对齐:居中;
}
img{
最大宽度:100%;
最大高度:100%;
}
/*标题*/
.标题{
背景色:#f1f1;
文本对齐:居中;
填充:20px;
字体系列:塔荷马,日内瓦,无衬线;
}
{%load static%}
我们仍在开发我们的网站,所以我们可能无法更新视频,因为我们想经常
将导航栏放在一个单独的文件中。可能称之为\u nav.html
然后在base.html
文件中,引用body标记开头的导航栏-
<body>
{% include '_nav.html' %}
.....
{% block content %}
{% endblock %}
</body>
{%include'\u nav.html%}
.....
{%block content%}
{%endblock%}
这样可以使
base.html
文件更干净。如果避免像在模板中构建导航这样的逻辑,效果会更好。好的做法是在上下文处理器中构建导航项,并在模板中显示它们
因此,首先需要创建context_processor,您可以在包含settings.py文件的同一文件夹中创建site_navigation.py文件
结构示例:
project/
...
settings.py
site_navigation.py
...
然后,您需要make函数(在您之前创建的site_navigation.py文件中),该函数将返回您的导航项目,在此函数中,您还可以创建逻辑,为授权用户附加一些特殊项目:
def navbar(request):
navbar_items = [
{'name': 'Home', 'link': '/'},
{'name': 'Link One', 'link': '/link-one'},
{'name': 'Link two', 'link': '/link-two'}
]
if request.user.is_authenticated():
navbar_items.append({'name': 'Link three', 'link': '/link-three'})
return {
'navbar_items': navbar_items
}
然后,您应该在设置文件中添加新的上下文处理器:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'project.site_navigation.navbar'
],
},
},
]
最后,需要在base.html文件中显示导航栏项目:
<ul>
{% for item in navbar_items %}
<li><a href="{{ item.link }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
{navbar_items%}
{%endfor%}
你说的“不是我网站上的每个网页都有视图”是什么意思?你如何在没有视图的情况下提供HTML??您还可以使用request.user.is\u authenticated
检查用户是否已登录。您的导航栏项目存放在哪里?在base.html中?这是硬代码吗?@AbdulAzizBarkat是的,我在没有视图的情况下提供了一些html。此外,我想根据他们是否注册了帐户而不是他们是否登录来决定放什么。@KIN1991我已将my base.html添加到question@KovyJacob我的问题是“在没有视图的情况下,如何提供HTML?”。你的评论根本没有回答这个问题。显示这些页面的url模式。。。如果用户未登录,您将如何确定他们是否拥有帐户??这是不可能神奇地发现,如果有人在您的网站帐户。。。