欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python Django模板的使用方法(圖文)

 更新時間:2013年11月04日 09:47:35   作者:  
模板通常用于產(chǎn)生HTML,但是Django的模板也能產(chǎn)生任何基于文本格式的文檔。

模版基本介紹
模板是一個文本,用于分離文檔的表現(xiàn)形式和內(nèi)容。 模板定義了占位符以及各種用于規(guī)范文檔該如何顯示的各部分基本邏輯(模板標(biāo)簽)。 模板通常用于產(chǎn)生HTML,但是Django的模板也能產(chǎn)生任何基于文本格式的文檔。
來一個項目說明
1、建立MyDjangoSite項目具體不多說,參考前面。
2、在MyDjangoSite(包含四個文件的)文件夾目錄下新建templates文件夾存放模版。
3、在剛建立的模版下建模版文件user_info.html

復(fù)制代碼 代碼如下:

<html>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>用戶信息</title>
    <head></head>
    <body>
        <h3>用戶信息:</h3>
        <p>姓名:{{name}}</p>
        <p>年齡:{{age}}</p>
    </body>
</html>

說明:{{ name }}叫做模版變量;{% if xx %} ,{% for x in list %}模版標(biāo)簽。

4、修改settings.py 中的TEMPLATE_DIRS
導(dǎo)入import os.path
添加 os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),

復(fù)制代碼 代碼如下:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

    #"E:/workspace/pythonworkspace/MyDjangoSite/MyDjangoSite/templates",
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)


說明:指定模版加載路徑。其中os.path.dirname(__file__)為當(dāng)前settings.py的文件路徑,再連接上templates路徑。

5、新建視圖文件view.py

復(fù)制代碼 代碼如下:

#vim: set fileencoding=utf-8:

#from django.template.loader import get_template
#from django.template import Context
#from django.http import HttpResponse
from django.shortcuts import render_to_response

def user_info(request):
    name = 'zbw'
    age = 24
    #t = get_template('user_info.html')
    #html = t.render(Context(locals()))
    #return HttpResponse(html)
    return render_to_response('user_info.html',locals())


說明:Django模板系統(tǒng)的基本規(guī)則: 寫模板,創(chuàng)建 Template 對象,創(chuàng)建 Context , 調(diào)用 render() 方法。
可以看到上面代碼中注釋部分
#t = get_template('user_info.html') #html = t.render(Context(locals()))
#return HttpResponse(html)
get_template('user_info.html'),使用了函數(shù) django.template.loader.get_template() ,而不是手動從文件系統(tǒng)加載模板。 該 get_template() 函數(shù)以模板名稱為參數(shù),在文件系統(tǒng)中找出模塊的位置,打開文件并返回一個編譯好的 Template 對象。
render(Context(locals()))方法接收傳入一套變量context。它將返回一個基于模板的展現(xiàn)字符串,模板中的變量和標(biāo)簽會被context值替換。其中Context(locals())等價于Context({'name':'zbw','age':24}) ,locals()它返回的字典對所有局部變量的名稱與值進(jìn)行映射。
render_to_response Django為此提供了一個捷徑,讓你一次性地載入某個模板文件,渲染它,然后將此作為 HttpResponse返回。

6、修改urls.py
 

復(fù)制代碼 代碼如下:

 from django.conf.urls import patterns, include, url
from MyDjangoSite.views import user_info

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'MyDjangoSite.views.home', name='home'),
    # url(r'^MyDjangoSite/', include('MyDjangoSite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),

    url(r'^u/$',user_info),

)
 

7、啟動開發(fā)服務(wù)器
基本一個簡單的模版應(yīng)用就完成,啟動服務(wù)看效果!
效果如圖:


模版的繼承
減少重復(fù)編寫相同代碼,以及降低維護(hù)成本。直接看應(yīng)用。
1、新建/templates/base.html

復(fù)制代碼 代碼如下:

<html>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>{% block title %}{% endblock %}</title>
    <head></head>
    <body>
        <h3>{% block headTitle %}{% endblock %}</h3>
        {% block content %} {% endblock %}

        {% block footer %}
            <h3>嘿,這是繼承了模版</h3>
        {% endblock%}
    </body>
</html>


2、修改/template/user_info.html,以及新建product_info.html
urser_info.html
復(fù)制代碼 代碼如下:

{% extends "base.html" %}

{% block title %}用戶信息{% endblock %}


<h3>{% block headTitle %}用戶信息:{% endblock %}</h3>

{% block content %}
<p>姓名:{{name}}</p>
<p>年齡:{{age}}</p>
{% endblock %}


product_info.html
復(fù)制代碼 代碼如下:

{% extends "base.html" %}
{% block title %}產(chǎn)品信息{% endblock %}
<h3>{% block headTitle %}產(chǎn)品信息:{% endblock %}</h3>
{% block content %}
    {{productName}}
{% endblock %}

3、編寫視圖邏輯,修改views.py
復(fù)制代碼 代碼如下:

#vim: set fileencoding=utf-8:

#from django.template.loader import get_template
#from django.template import Context
#from django.http import HttpResponse
from django.shortcuts import render_to_response

def user_info(request):
    name = 'zbw'
    age = 24
    #t = get_template('user_info.html')
    #html = t.render(Context(locals()))
    #return HttpResponse(html)
    return render_to_response('user_info.html',locals())

def product_info(request):
    productName = '阿莫西林膠囊'
    return render_to_response('product_info.html',{'productName':productName})

4、修改urls.py

復(fù)制代碼 代碼如下:

from django.conf.urls import patterns, include, url
from MyDjangoSite.views import user_info,product_info

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'MyDjangoSite.views.home', name='home'),
    # url(r'^MyDjangoSite/', include('MyDjangoSite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),

    url(r'^u/$',user_info),
    url(r'^p/$',product_info),
)

5、啟動服務(wù)效果如下:

相關(guān)文章

  • 手把手教你如何安裝Pycharm(詳細(xì)圖文教程)

    手把手教你如何安裝Pycharm(詳細(xì)圖文教程)

    這篇文章主要介紹了手把手教你如何安裝Pycharm(詳細(xì)圖文教程),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • python flask搭建web應(yīng)用教程

    python flask搭建web應(yīng)用教程

    今天小編就為大家分享一篇python flask搭建web應(yīng)用教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • opencv-python圖像處理安裝與基本操作方法

    opencv-python圖像處理安裝與基本操作方法

    OpenCV是一個C++庫,目前流行的計算機(jī)視覺編程庫,用于實時處理計算機(jī)視覺方面的問題,它涵蓋了很多計算機(jī)視覺領(lǐng)域的模塊。在Python中常使用OpenCV庫實現(xiàn)圖像處理。本文介紹opencv-python圖像處理安裝與基本操作,感興趣的朋友一起看看吧
    2022-01-01
  • 使用Termux在手機(jī)上運行Python的詳細(xì)過程

    使用Termux在手機(jī)上運行Python的詳細(xì)過程

    這篇文章主要介紹了使用Termux在手機(jī)上運行Python的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • 使用Python實現(xiàn) 學(xué)生學(xué)籍管理系統(tǒng)

    使用Python實現(xiàn) 學(xué)生學(xué)籍管理系統(tǒng)

    這篇文章主要介紹了使用Python實現(xiàn) 學(xué)生學(xué)籍管理系統(tǒng),代碼大致分為五個函數(shù)組成,具體內(nèi)容詳情本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-11-11
  • python實現(xiàn)簡單五子棋小游戲

    python實現(xiàn)簡單五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡單五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • python列表:開始、結(jié)束、步長值實例

    python列表:開始、結(jié)束、步長值實例

    這篇文章主要介紹了python列表:開始、結(jié)束、步長值實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python3爬蟲中引用Queue的實例講解

    python3爬蟲中引用Queue的實例講解

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python3爬蟲中引用Queue的實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-11-11
  • Pandas中Series的屬性,方法,常用操作使用案例

    Pandas中Series的屬性,方法,常用操作使用案例

    這篇文章主要介紹了Pandas中Series的屬性,方法,常用操作使用案例,文章通過包的引入展開主題,需要的朋友可以參考一下
    2022-07-07
  • 通過5個例子讓你學(xué)會Pandas中的字符串過濾

    通過5個例子讓你學(xué)會Pandas中的字符串過濾

    毋庸置疑Pandas是使用最廣泛的Python庫之一,它提供了許多功能和方法來執(zhí)行有效的數(shù)據(jù)處理和數(shù)據(jù)分析,下面這篇文章主要給大家介紹了關(guān)于如何通過5個例子讓你學(xué)會Pandas中字符串過濾的相關(guān)資料,需要的朋友可以參考下
    2022-08-08

最新評論