温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

django url和返回内容到html

发布时间:2020-07-16 15:20:57 来源:网络 阅读:3204 作者:狮子XL 栏目:开发技术

文章思维导图

django url和返回内容到html


nav.html, bottom.html, tongji.html


base.html

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}默认标题{% endblock %} - lanny教堂</title>
</head>
<body>
{% include 'nav.html' %}
//定义块,以后模版继承base后可以替换.
{% block content %}
<div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>
{% endblock %}
 
{% include 'bottom.html' %}
{% include 'tongji.html' %}
</body>
</html>


home.html继承且覆盖base

{% extends 'base.html' %}

{% block title %}欢迎光临首页{% endblock %}

{% block content %}
{% include 'ad.html' %}
这里是首页,欢迎光临
{% endblock %}


包含语法

{% include 'bottom.html' %}

{% block content %}
{% include 'ad.html' %}
{% endblock %}



字符串

views.py
def home(request):
    string = u"我在lanny教堂学习Django,用它来建网站"
    return render(request, 'home.html', {'string': string})
home.html
{{ string }}


列表:

views.py
def home(request):
    TutorialList = ["HTML", "CSS", "jQuery", "Python", "Django"]
    return render(request, 'home.html', {'TutorialList': TutorialList})
在视图中我们传递了一个List到模板 home.html:
教程列表:
{% for i in TutorialList %}
{{ i }}
{% endfor %}

home.html

教程列表:

{% for i in TutorialList %}
{{ i }}
{% endfor %}


字典

views.py

def home(request):
    info_dict = {'site': u'lanny教堂', 'content': u'各种IT技术教程'}
    return render(request, 'home.html', {'info_dict': info_dict})
home.html

法1:

站点:{{ info_dict.site }} 内容:{{ info_dict.content }}
法2:遍历
{% for key, value in info_dict.items %}
    {{ key }}: {{ value }}
{% endfor %}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI