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

python Django模板的使用方法

 更新時(shí)間:2016年01月14日 10:44:57   投稿:lijiao  
這篇文章主要為大家介紹了python Django模板的使用方法,代碼很詳細(xì),感興趣的小伙伴們可以參考一下

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

<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(‘\\','/'),

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

#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() ,而不是手動(dòng)從文件系統(tǒng)加載模板。 該 get_template() 函數(shù)以模板名稱為參數(shù),在文件系統(tǒng)中找出模塊的位置,打開文件并返回一個(gè)編譯好的 Template 對象。
render(Context(locals()))方法接收傳入一套變量context。它將返回一個(gè)基于模板的展現(xiàn)字符串,模板中的變量和標(biāo)簽會(huì)被context值替換。其中Context(locals())等價(jià)于Context({‘name':'zbw','age':24}) ,locals()它返回的字典對所有局部變量的名稱與值進(jìn)行映射。
render_to_response Django為此提供了一個(gè)捷徑,讓你一次性地載入某個(gè)模板文件,渲染它,然后將此作為 HttpResponse返回。

6、修改urls.py

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、啟動(dòng)開發(fā)服務(wù)器

基本一個(gè)簡單的模版應(yīng)用就完成,啟動(dòng)服務(wù)看效果!
效果如圖:

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

<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

{% extends "base.html" %}
{% block title %}用戶信息{% endblock %}
 
<h3>{% block headTitle %}用戶信息:{% endblock %}</h3>
{% block content %}
<p>姓名:{{name}}</p>
<p>年齡:{{age}}</p>
{% endblock %}

product_info.html

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

3、編寫視圖邏輯,修改views.py

#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

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、啟動(dòng)服務(wù)效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • 在Python中執(zhí)行異常處理的基本步驟

    在Python中執(zhí)行異常處理的基本步驟

    異常處理是編寫健壯、可靠和易于調(diào)試的Python代碼中不可或缺的一部分,下面這篇文章主要給大家介紹了關(guān)于在Python中執(zhí)行異常處理的基本步驟,需要的朋友可以參考下
    2024-08-08
  • python多線程抽象編程模型詳解

    python多線程抽象編程模型詳解

    這篇文章主要為大家詳細(xì)介紹了python多線程抽象編程模型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Python爬蟲設(shè)置ip代理過程解析

    Python爬蟲設(shè)置ip代理過程解析

    這篇文章主要介紹了Python爬蟲設(shè)置ip代理過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • python批量生成條形碼的示例

    python批量生成條形碼的示例

    這篇文章主要介紹了python批量生成條形碼的示例,幫助大家更好的利用python處理圖形,感興趣的朋友可以了解下
    2020-10-10
  • OpenCV+python3實(shí)現(xiàn)視頻分解成圖片

    OpenCV+python3實(shí)現(xiàn)視頻分解成圖片

    這篇文章主要為大家詳細(xì)介紹了OpenCV+python3實(shí)現(xiàn)視頻分解成圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見鏈接方式

    詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見鏈接方式

    這篇文章主要介紹了詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見鏈接方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 如何一鍵理清大型Python項(xiàng)目依賴樹

    如何一鍵理清大型Python項(xiàng)目依賴樹

    這篇文章主要介紹了如何一鍵理清大型Python項(xiàng)目依賴樹,文章圍繞主題相關(guān)資料展開詳細(xì)的內(nèi)容介紹,感興趣的小伙伴可以參考一下
    2022-06-06
  • python之流程控制語句match-case詳解

    python之流程控制語句match-case詳解

    這篇文章主要介紹了python之流程控制語句match-case使用,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Python的Import機(jī)制的模塊與包深入理解

    Python的Import機(jī)制的模塊與包深入理解

    深入理解Python的import機(jī)制有助于更好地組織代碼、提高代碼復(fù)用性,本文將深入研究Python的Import機(jī)制,包括模塊的導(dǎo)入過程、命名空間與作用域、相對導(dǎo)入以及包的結(jié)構(gòu)和導(dǎo)入等方面,通過豐富的示例代碼,助你更全面地理解和應(yīng)用這
    2024-01-01
  • Python應(yīng)用領(lǐng)域和就業(yè)形勢分析總結(jié)

    Python應(yīng)用領(lǐng)域和就業(yè)形勢分析總結(jié)

    在本篇文章總我們給大家整理了關(guān)于Python應(yīng)用領(lǐng)域和就業(yè)形勢分析以及圖文介紹,需要的朋友們可以參考下。
    2019-05-05

最新評論

宁海县| 枝江市| 南丰县| 登封市| 内江市| 庄河市| 宣武区| 长汀县| 潞城市| 肇源县| 胶南市| 广东省| 锦州市| 朔州市| 南华县| 乡城县| 新余市| 教育| 博野县| 南木林县| 平昌县| 会昌县| 车险| 贵阳市| 南城县| 富平县| 克山县| 高邑县| 太湖县| 夏河县| 调兵山市| 西昌市| 青州市| 盐边县| 呼图壁县| 会昌县| 宜城市| 荥阳市| 鹿邑县| 新源县| 永济市|