欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python Django模板的使用方法

系統 1902 0

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

            
            

用戶信息:

姓名:{{name}}

年齡:{{age}}

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

4、修改settings.py 中的TEMPLATE_DIRS
導入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__)為當前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模板系統的基本規則: 寫模板,創建 Template 對象,創建 Context , 調用 render() 方法。

可以看到上面代碼中注釋部分
#t = get_template(‘user_info.html') #html = t.render(Context(locals()))
#return HttpResponse(html)
get_template(‘user_info.html'),
使用了函數 django.template.loader.get_template() ,而不是手動從文件系統加載模板。 該 get_template() 函數以模板名稱為參數,在文件系統中找出模塊的位置,打開文件并返回一個編譯好的 Template 對象。
render(Context(locals()))方法接收傳入一套變量context。它將返回一個基于模板的展現字符串,模板中的變量和標簽會被context值替換。其中Context(locals())等價于Context({‘name':'zbw','age':24}) ,locals()它返回的字典對所有局部變量的名稱與值進行映射。
render_to_response Django為此提供了一個捷徑,讓你一次性地載入某個模板文件,渲染它,然后將此作為 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、啟動開發服務器

基本一個簡單的模版應用就完成,啟動服務看效果!
效果如圖:

python Django模板的使用方法_第1張圖片

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

            
            

{% block headTitle %}{% endblock %}

{% block content %} {% endblock %} {% block footer %}

嘿,這是繼承了模版

{% endblock%}

2、修改/template/user_info.html,以及新建product_info.html
urser_info.html

            
{% extends "base.html" %}
{% block title %}用戶信息{% endblock %}
 

            

{% block headTitle %}用戶信息:{% endblock %}

{% block content %}

姓名:{{name}}

年齡:{{age}}

{% endblock %}

product_info.html

            
{% extends "base.html" %}
{% block title %}產品信息{% endblock %}

            

{% block headTitle %}產品信息:{% endblock %}

{% 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、啟動服務效果如下:

python Django模板的使用方法_第2張圖片

以上就是本文的全部內容,希望對大家的學習有所幫助。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美日韩国产精品一区二区 | 夜精品A片观看无码一区二区 | 污在线观看| 91在线最新 | 美乳在线观看 | 日韩视频观看 | 2022国产成人精品福利网站 | 亚洲一区二区三区福利在线 | 国产在线观看福利片 | 国产高清精品一区二区三区 | 欧美激情第二页 | 亚洲毛片在线观看 | 哥斯拉大战金刚2在线观看免费完整版 | 久久久无码精品一区二区三区 | 亚洲欧美日韩在线线精品 | 久草在线视频资源 | 欧美成人另类人妖 | 九九黄色| 亚洲精品手机在线 | 日韩www| 国产黄的网站免费 | 九九综合九九 | 欧美一级永久免费毛片在线 | 成人午夜电影网 | 久草免费在线 | 亚洲综合国产一区二区三区 | 九色成人蝌蚪国产精品电影在线 | 牛牛碰在线视频 | 国产成人黄网址在线视频 | 欧美精品第一页 | 又大又粗进出白浆直流动态图 | 在线中文字幕日韩 | 欧美交换乱理伦片120秒 | 日韩av成人 | 欧美日色 | 99久久精品国产一区二区三区 | 亚洲无线一二三四手机 | 亚洲精品久久一区二区三区四区 | 国产精品久久久久久久免费 | 久久亚洲日本不卡一区二区 | 男女无遮挡高清性视频直播 |