溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

簡單演示django使用四

發(fā)布時間:2020-08-11 18:41:32 來源:網(wǎng)絡(luò) 閱讀:501 作者:crystaleone 欄目:開發(fā)技術(shù)

代碼環(huán)境接使用三。


先在django中定義模板:


第一步:在項目所在的當前目錄創(chuàng)建templates模板目錄

    mkdir templates


第二步:在templates目錄下,創(chuàng)建與應(yīng)用同名的目錄即應(yīng)用的模板目錄:

    mkdir templates/bookshop


第三步:在應(yīng)用的模板目錄下創(chuàng)建index.html文件

    vim templates/bookshop/index.html

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>index</title>

</head>

<body>

    <h2>this is django templates index.html</he>

</body>

</html>

注意:此html模板文件里可以使用模板語言{% keyworld %}、{{ object }}。


定義模板完成后再使用模板:


第四步:在views.py視圖中使用模板

編輯bookshop/views.py文件:

修改為:

#from django.shortcuts import render

from django.http import *

from django.template import RequestContext,loader

def index(request):

    temp = loader.get_template('bookshop/index.html')

    return HttpResponse(temp.render())

    #return HttpResponse('<b>hello world</b>')


或把loader加載過程和宣傳過程,由render完成簡化了代碼,另一種寫法為:

from django.shortcuts import render

from django.http import *

def index(request):

    return render(request, 'bookshop/index.html')


第五步:在settings.py設(shè)置中,添加模板查找路徑

編輯test1/settings.py文件:

修改:

TEMPLATES = [

    {

        ...

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

    ...}]


此時,訪問瀏覽器:http://192.168.255.70:8080/

簡單演示django使用四


獲取數(shù)據(jù)并傳遞數(shù)據(jù):

在視圖views.py中,render中第一個參數(shù)是請求,第二個參數(shù)是使用的htim模板文件,第三個參數(shù)要傳遞的數(shù)據(jù)必須使用字典格式:


第一步:向模板中傳遞數(shù)據(jù)

編輯bookshoop/views.py文件

修改:

from django.shortcuts import render

from django.http import *

from .models import *

def index(request):

    booklist = BookInfo.objects.all()

    context = {'list':booklist}

    return render(request, 'bookshop/index.html', context)


第二步:在模板中獲取數(shù)據(jù)

編輯templates/bookshop/index.html

修改如下:

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>index</title>

</head>

<body>

    {%for book in list %}

    <li>{{book.btitle}}</li>

    {%endfor%}

    <h2>this is django templates index.html</h2>

</body>

</html>


第三步:刷新瀏覽器

訪問瀏覽器:http://192.168.255.70:8080/

簡單演示django使用四


使用更多模板語言:

第一步:編輯templates/bootshop/index.html模板文件

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>index</title>

</head>

<body>

    <ul>

        {%for book in list%}

        <li><a href="{{book.id}}">{{book.btitle}}</a></li>

        {%endfor%}

    </ul>

    <h2>this is django templates index.html</h2>

</body>

</html>


第二步:修改url路由配置

編輯bookshop/urls.py文件:

from django.conf.urls import url

from . import views

urlpatterns = [

    url(r'^$',views.index),

    url(r'^(\d+)$',views.show),#正則匹配到的結(jié)果是當做一個show的參數(shù)

]


第三步:添加視圖函數(shù)

編輯bookshop/views.py文件:

添加:


def show(request,id):#該函數(shù)一共要2個參數(shù),其中id為正則式傳遞的參數(shù)

    book = BookInfo.objects.get(pk=id)

    herolist = book.heroinfo_set.all()

    context = {'list':herolist`}

    return render(request,'bookshop/show.html',context)


第四步:添加show.html模板文件

編輯templates/bookshop/show.html文件:


<!DOCTYPE html>

<html lange="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

    <ul>

        {%for hero in list%}

        <li>{{hero.hname}}</li>

        {%endfor%}

    </ul>

</body>

</html>


第五步:測試

瀏覽器訪問:http://192.168.255.70:8080/

簡單演示django使用四

為測試添加數(shù)據(jù):

瀏覽器訪問:http://192.168.255.70:8080/admin/

添加Hero infos信息,點擊Hero infos:

簡單演示django使用四

然后,點擊:增加hero info


簡單演示django使用四

填寫hero info相關(guān)信息即可:

簡單演示django使用四

填寫完成后,保存即可。


瀏覽器訪問測試:http://192.168.255.70:8080/

簡單演示django使用四

單擊鏈接標簽,如python,注意瀏覽器地址的變化為:http://192.168.255.70:8080/2

簡單演示django使用四


測試成功。








向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI