django創(chuàng)建項(xiàng)目案例1使用模板續(xù)03

模板

  • 模板是html頁面,可以根據(jù)視圖中傳遞的數(shù)據(jù)填充值
  • 創(chuàng)建模板的目錄如下圖:


    image.png
image.png
  • 修改settings.py文件,設(shè)置TEMPLATES的DIRS值

'DIRS': [os.path.join(BASE_DIR, 'templates')],

  • index.html代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello world</h1>

</body>
</html>
  • 修改views.py文件代碼:
#coding:utf-8
# from django.shortcuts import render
from django.http import *
from django.template import RequestContext,loader

# Create your views here.
def index(request):
    temp = loader.get_template('booktest/index.html')
    return HttpResponse(temp.render())
  • 修改settings.py文件代碼如下:
ROOT_URLCONF = 'test1.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
  • 在終端啟動python manage.py runserver


    image.png
  • 效果圖:

image.png

修改views.py文件代碼同樣也可以獲得以上效果代碼量也減少:

#coding:utf-8
from django.shortcuts import render
from django.http import *
# from django.template import RequestContext,loader

# Create your views here.
def index(request):
    # temp = loader.get_template('booktest/index.html')
    # return HttpResponse(temp.render())
    return render(request,'booktest/index.html')
  • 效果圖:

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 切換到創(chuàng)建項(xiàng)目的目錄 cd C:\Users\admin\Desktop\DjangoProject創(chuàng)建名為pr...
    在努力中閱讀 3,530評論 2 3
  • 模塊間聯(lián)系越多,其耦合性越強(qiáng),同時表明其獨(dú)立性越差( 降低耦合性,可以提高其獨(dú)立性)。軟件設(shè)計中通常用耦合度和內(nèi)聚...
    riverstation閱讀 2,212評論 0 8
  • (一)、啟動服務(wù)器 (二)、創(chuàng)建數(shù)據(jù)庫表 或 更改數(shù)據(jù)庫表或字段 Django 1.7.1及以上 用以下命令 1....
    夏天夏星閱讀 5,940評論 0 17
  • Django是一個用 Python 編寫的 Web 框架。Web 框架是一種軟件,基于web框架可以開發(fā)動態(tài)網(wǎng)站,...
    guanalex閱讀 7,496評論 0 5
  • 1 滿月初上。 簡寧盤著腿坐在電腦前,鼠標(biāo)光標(biāo)落在“發(fā)送”兩個字上,單擊左鍵,將今天的文章推送了出去。 今天寫的是...
    莞小主閱讀 984評論 1 5

友情鏈接更多精彩內(nèi)容