最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

模版基本介紹
模板是一個文本,用于分離文檔的表現(xiàn)形式和內(nèi)容。 模板定義了占位符以及各種用于規(guī)范文檔該如何顯示的各部分基本邏輯(模板標簽)。 模板通常用于產(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 %}模版標簽。

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__)為當前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)字符串,模板中的變量和標簽會被context值替換。其中Context(locals())等價于Context({'name':'zbw','age':24}) ,locals()它返回的字典對所有局部變量的名稱與值進行映射。
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ù)編寫相同代碼,以及降低維護成本。直接看應(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)文章

  • Python錯誤NameError:name?'X'?is?not?defined的解決方法

    Python錯誤NameError:name?'X'?is?not?defined的解決方法

    這篇文章主要給大家介紹了關(guān)于Python錯誤NameError:name?‘X‘?is?not?defined的解決方法,這是最近工作中遇到的一個問題,文中通過實例代碼將解決的方法介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • Pandas 稀疏數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)

    Pandas 稀疏數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)

    如果數(shù)據(jù)中有很多NaN的值,存儲起來就會浪費空間。為了解決這個問題,Pandas引入了一種叫做Sparse data的結(jié)構(gòu),來有效的存儲這些NaN的值,本文就來詳細的介紹了一下,感興趣的可以了解一下
    2021-07-07
  • Python HTTP庫 requests 的簡單使用詳情

    Python HTTP庫 requests 的簡單使用詳情

    requests是Python的一個HTTP客戶端庫,基于urllib標準庫,在urllib標準庫的基礎(chǔ)上做了高度封裝,因此更加簡潔好用,下面就由小編來給大家詳細介紹吧,需要的朋友可以參考下
    2021-09-09
  • Python+PyQt5實現(xiàn)自動點擊神器

    Python+PyQt5實現(xiàn)自動點擊神器

    這篇文章主要為大家詳細介紹了如何利用Python和PyQt5實現(xiàn)自動點擊神器,旨在解決重復(fù)性的點擊工作,解放雙手,具有及時性和準確性,需要的可以參考下
    2024-01-01
  • Python簡單實現(xiàn)的代理服務(wù)器端口映射功能示例

    Python簡單實現(xiàn)的代理服務(wù)器端口映射功能示例

    這篇文章主要介紹了Python簡單實現(xiàn)的代理服務(wù)器端口映射功能,結(jié)合實例形式分析了Python模擬服務(wù)器、代理服務(wù)器及客戶端訪問的相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • python中關(guān)于tqdm的用法

    python中關(guān)于tqdm的用法

    這篇文章主要介紹了python中關(guān)于tqdm的用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • NumPy中np.random.rand函數(shù)的實現(xiàn)

    NumPy中np.random.rand函數(shù)的實現(xiàn)

    np.random.rand是NumPy庫中的一個函數(shù),用于生成隨機數(shù),本文主要介紹了NumPy中np.random.rand函數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • python 多線程與多進程效率測試

    python 多線程與多進程效率測試

    這篇文章主要介紹了python 多線程與多進程效率測試,在Python中,計算密集型任務(wù)適用于多進程,IO密集型任務(wù)適用于多線程、接下來看看文章得實例吧,需要的朋友可以參考一下喲
    2021-10-10
  • Python實現(xiàn)向QQ群成員自動發(fā)郵件的方法

    Python實現(xiàn)向QQ群成員自動發(fā)郵件的方法

    這篇文章主要介紹了Python實現(xiàn)向QQ群成員自動發(fā)郵件的方法,通過讀取txt文本里的QQ成員數(shù)據(jù)再調(diào)用發(fā)送郵件函數(shù)實現(xiàn)該功能,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • linux之文件查找指定文件中包含關(guān)鍵字的行信息方式

    linux之文件查找指定文件中包含關(guān)鍵字的行信息方式

    這篇文章主要介紹了linux之文件查找指定文件中包含關(guān)鍵字的行信息方式,具有很好的參考價值,希望對大家有所幫助。
    2023-06-06

最新評論

嘉黎县| 古浪县| 皋兰县| 呈贡县| 延庆县| 平凉市| 布拖县| 静安区| 漳浦县| 阳西县| 丁青县| 永州市| 桃源县| 防城港市| 丰都县| 云浮市| 巨野县| 祁阳县| 汉寿县| 泰来县| 尼木县| 台东县| 延安市| 探索| 扬州市| 体育| 武清区| 岳阳市| 越西县| 永修县| 比如县| 大名县| 犍为县| 陆丰市| 乌拉特后旗| 绵竹市| 江油市| 商城县| 望城县| 凤庆县| 榆中县|