• Django 模板


    Django 模板:
    
    在前面的几节中我们都是简单的django.http.HttpResponse 来把内容显示到网页上,
    
    本节将讲解如何使用渲染模板的方法来显示内容。
    
    本节代码是基于Django 1.8,但Django 1.4 - Django 1.11 操作流程也一样的。
    
    >>> import django
    >>> print django.__version__
    1.11
    
    
    1.创建一个zqxt_tmpl 项目,和一个 名称为 learn 的应用,并且
    
    node2:/app#django-admin.py startproject zqxt_tmpl
    node2:/app#cd zqxt_tmpl/
    node2:/app/zqxt_tmpl#python manage.py startapp learn
    node2:/app/zqxt_tmpl#
    
    2.把learn 加入到settings.INSTALLED_APPS中
    
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
     
        'learn',
    )
    
    
    3.打开laern/views.py 写一个首页的视图
    
    from django.shortcuts import render
     
     
    def home(request):
        return render(request, 'home.html')
    
    
    
    
    4.在learn 目录下新建一个templates文件夹,
    
    
    里面新建一个home.html
    
    
    默认配置下,Django 的模板系统会自动找到app下面的templates文件夹中的模板文件。
    
    
    5.在home.html 中写一些内容:
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>欢迎光临</title>
    </head>
    <body>
    欢迎光临自强学堂
    </body>
    </html>
    
    6.注意:Django 1.10.x 中为
    
    1
    url(r'^admin/', admin.site.urls),
    
    
    node2:/app/zqxt_tmpl/learn#cat views.py
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    from django.shortcuts import render
    
    # Create your views here.
    from django.shortcuts import render
     
     
    def home(request):
        return render(request, 'home.html')
    
    
    
    8.运行开发服务器,看看效果
    
    
    
    模板补充知识:
    
    网站模板的设计,一般的,我们做网站由一些通用的部分,比如导航,底部,访问统计代码等待。
    
    
    可以写一个base.html 来包含这些通用文件(include):
    
    node2:/app/zqxt_tmpl/learn/templates#cat base.html 
    <!DOCTYPE html>
    <html>
    <head>
        <title>{% block title %}默认标题{% endblock %} - 自强学堂</title>
    </head>
    <body>
     
    {% include 'nav.html' %}
     
    {% block content %}
    <div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>
    {% endblock %}
     
    {% include 'bottom.html' %}
     
    {% include 'tongji.html' %}
     
    </body>
    </html>
    
    
    如果需要,写足够多的block以便继承的模板可以重写该部分,include是包含其它文件的内容,
    
    就是把一些网页公用的部分拿出来,重复利用
    
    
    
    注意: 模板一般放在app下的templates中,Django会自动去找这个文件夹中找。
    
    但假如我们每个app的templates中都有一个index.html,当我们在views.py中使用的时候,
    
    直接写一个render(request,'index.html'),django能不能找到当前app的templates文件夹中的
    
    index.html文件夹呢?(答案是不一定能,有可能找错)
    
    
    Django 模板查找机制:Django查找模板的过程是在每个app的templates文件夹中找
    
    (而不是只在当前app中的代码只在当前的app的templates文件夹中找)

  • 相关阅读:
    20070521_新疆出差纪实
    有想应聘技术咨询顾问职位的朋友请看过来
    Access:運作必須查詢3/19
    我們是好朋友1/3
    小說學習1/22
    [Asp.net]動態設定標簽寬度2/22
    [Asp.net]HyperLinkColumn應用2/28
    [Asp.net]重啟IIS2/29
    [Asp.NET]水晶報表與網路密碼2/27
    把學習量化3/6
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349380.html
Copyright © 2020-2023  润新知