Django模板标签

Django模板标签

接上一节 https://blog.youkuaiyun.com/LeeSkyRa/article/details/103735329

相对上一节,app_st/views.py 文件和 templates/app_st/variable.py 文件代码做了部分删除和修改,并在 templates/app_st 目录下新建了一个广告模板 ad.html 文件

templates/app_st/ad.html 文件代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>广告</title>
    <style>
        a{
            text-decoration: none;
        }
        a:hover{
            text-decoration: none;
        }
        #box_ad{
            position: fixed;
            bottom: 0;
        }
    </style>
</head>
<body>
<div id="box_ad">
    <h1><a id="a1" href="http://www.taobao.com">
        我是某宝广告!不要点,不要点!</a></h1>
</div>
<script>
    var h=document.getElementById('a1');
    var color='blue';
    function change_color() {
        if (color=='blue'){
            color='red';
        }else {
            color='blue';
        }
        h.style.color=color;
        setTimeout('change_color()',200)
    }
    change_color()
</script>
</body>
</html>

app_st/views.py 文件代码如下:

from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime
# Create your views here.

def view_st(request):
    return HttpResponse('<h1>这是我的第一个 django 视图</h1>')

def home(request):
    return render(request,'app_st/home.html')

def base(request):
    return render(request,'app_st/base.html')



def variable(request):
    lee={
        'name':'天兔',
        'age':20,
        'sex':'M',
    }
    li=[1,2,3,4,5,6,7,8,9,0]
    return render(request,'app_st/variable.html',context={
        'lee':lee,
        'li':li,
    })

templates/app_st/variable.py 文件代码如下:

{% extends 'app_st/base.html' %}
{% load myfilter %}
{% load static %}
{% block title %}VARIABLE{% endblock %}
{% block css %}{% static 'app_st/css/variable.css' %}{% endblock %}
{% block content %}
    <div id="box_content">
        {% for key,val in lee.items %}
            <p>我的 {{ key }} 是 : {{ val }}</p>
        {% endfor %}
        <hr>
        {% for a in li %}
            {% if a > 0 %}
                <p>列表第 {{ a }} 个元素是 : {{ a }}</p>
            {% elif a == 0 %}
                <p>列表第 {{ a | add:10 }} 个元素是 : {{ a }}</p>
            {% endif %}
        {% endfor %}
    </div>
{% endblock %}
{% block ad %}
    {% include 'app_st/ad.html' %}
{% endblock %}

下面,启动 django 服务,浏览器地址栏输入 127.0.0.1:8000/app/variable 查看效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值