django的環(huán)境配置和view的使用-創(chuàng)新互聯(lián)

這篇文章主要介紹了django的環(huán)境配置和view的使用 ,具有一定借鑒價值,需要的朋友可以參考下。步驟簡單適合新手,希望你能收獲更多。下面是配置和使用的步驟內(nèi)容。

10多年的滎經(jīng)網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整滎經(jīng)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“滎經(jīng)網(wǎng)站設(shè)計”,“滎經(jīng)網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

一 基本環(huán)境

1 環(huán)境處理

mkdir  djanad cd djanad/ pyenv  virtualenv 3.6.5  djanad pyenv  local  djanad

結(jié)果如下

django的環(huán)境配置和view的使用

2  創(chuàng)建django和基本配置

 pip install  django==2.1
django-admin startproject  demo . django-admin  startapp  app

結(jié)果如下

django的環(huán)境配置和view的使用

數(shù)據(jù)庫配置如下

django的環(huán)境配置和view的使用

基本時區(qū)和mysql配置及相關(guān)時區(qū)配置請看django基礎(chǔ)

啟動結(jié)果如下

django的環(huán)境配置和view的使用

二  view基本使用

1  view中使用模板

1  概述

django內(nèi)置了自己的模板引擎,和jinjia 很像,使用簡單

使用 Template 進行定義模板,使用Context 將數(shù)據(jù)導入到該模板中,其導入默認使用字典

django的環(huán)境配置和view的使用

2 環(huán)境準備

1 創(chuàng)建models

django 默認會去到app_name/templates下尋找模板,這是settings中的默認設(shè)置,默認會去app_name/static找那個尋找靜態(tài)文件(css,js,jpg,html)等


在  app/models.py 中創(chuàng)建數(shù)據(jù)庫表模板,具體配置如下:

from django.db import models # Create your models here. # 問題 class Question(models.Model):    question_text = models.CharField(max_length=200)    pub_date = models.DateTimeField('date published')    def __str__(self):      return self.question_text # 選擇 # 配置選擇為問題的外鍵,并配置選擇的內(nèi)容和選擇的起始值 class Choice(models.Model):    question = models.ForeignKey(Question, on_delete=Question)    choice_text = models.CharField(max_length=200)    votes = models.IntegerField(default=0)    def __str__(self):      return self.choice_text
2 執(zhí)行生成遷移文件和遷移并查看
 python manage.py  makemigrations  python manage.py  migrate

結(jié)果如下

django的環(huán)境配置和view的使用

3 添加數(shù)據(jù)進入表中

創(chuàng)建后臺登陸用戶,設(shè)置用戶名為admin,密碼為admin@123

django的環(huán)境配置和view的使用

4 將model中的模型添加進入django admin 后臺管理界面

app/admin.py中添加

# Register your models here. from django.contrib import admin from .models import Question, Choice # Register your models here. class ChoiceInline(admin.TabularInline):    model = Choice    extra = 3 class QuestionAdmin(admin.ModelAdmin):    fieldsets = [      (None, {'fields': ['question_text']}),      ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),    ]    inlines = [ChoiceInline]    list_display = ('question_text', 'pub_date') admin.site.register(Choice) admin.site.register(Question, QuestionAdmin)

url  :  localhost:port/admin/

5 登陸后臺并添加數(shù)據(jù)如下

django的環(huán)境配置和view的使用

django的環(huán)境配置和view的使用

6 配置靜態(tài)文件

demo/setting.py 中配置添加

STATICFILES_DIRS = [    os.path.join(BASE_DIR, 'static') ]

項目中創(chuàng)建static 并上傳圖片django.jpg

django的環(huán)境配置和view的使用

7  配置 url

demo/urls.py中配置如下

from django.conf.urls import url, include from django.contrib import admin urlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^app/', include("app.urls",namespace="app")),  #此處配置名稱空間,用于處理后面的翻轉(zhuǎn) ]
8  app中創(chuàng)建  urls.py 文件,內(nèi)容如下
from django.conf.urls import url, include from . import views urlpatterns = [    url(r'^index/$', views.index, name="index"), # name 指定名稱, ]

django的環(huán)境配置和view的使用

3 view 使用

1 在view中直接嵌入模板,結(jié)果如下
from django.shortcuts import render from django.template import Template, Context from . import models from django.http import HttpResponse # Create your views here. def index(request):    lastes_question_list = models.Question.objects.order_by('-pub_date')[:5]    template = Template("""    <img  src="/static/django.jpg">    {%  if lastes_question_list %}    <ul>    {% for question  in  lastes_question_list %}    <li>  <a  href="/app/ {{question.id}}/"> {{ question.question_text }} </a> </li>    {% endfor %}    </ul>    {% endif %}    """)    context = Context({"lastes_question_list": lastes_question_list})    return HttpResponse(template.render(context))

訪問配置,結(jié)果如下

django的環(huán)境配置和view的使用

2 使用html 模板如下

django的環(huán)境配置和view的使用

index 代碼如下

<!DOCTYPE html> <html> <head>    <meta charset="UTF-8">    <title>測試數(shù)據(jù)</title> </head> <body> <img src="/static/django.jpg"> {% if lastes_question_list %} <ul>    {% for question in lastes_question_list %}    <li>      <a href="/app/{{question.id}}/"> {{question.question_text}} </a>    </li>    {% endfor %} </ul> {% endif%} </body> </html>

app/view.py 中代碼如下

from . import models from django.http import HttpResponse from django.template import loader # Create your views here. def index(request):    lastes_question_list = models.Question.objects.order_by('-pub_date')[:5]    template = loader.get_template("app/index.html")    context = {"lastes_question_list": lastes_question_list}    return HttpResponse(template.render(context))
3 index.html不變,app/view 修改
from . import models from django.shortcuts import render # Create your views here. def index(request):    lastes_question_list = models.Question.objects.order_by('-pub_date')[:5]    context = {"lastes_question_list": lastes_question_list}    return render(request, template_name="app/index.html", context=context)
4 去掉static 和 url中的硬編碼及反向解析

根據(jù)根路由中注冊的namespace和子路由中注冊的name來動態(tài)獲取路徑。在模板中使用"{% url  namespace:name %}"
如果攜帶位置參數(shù) 
“{% url  namespace:name  args %}"
如果攜帶關(guān)鍵字參數(shù) 
“{% url  namespace:name  k1=v1  k2=v2  %}"


配置 詳情頁面添加數(shù)據(jù)

app/view.py 中添加數(shù)據(jù)如下

from . import models from django.shortcuts import render # Create your views here. def index(request):    lastes_question_list = models.Question.objects.order_by('-pub_date')[:5]    context = {"lastes_question_list": lastes_question_list}    return render(request, template_name="app/index.html", context=context) def detal(request, question_id):    detal = models.Question.objects.get(pk=question_id)    context = {"detal": detal}    return render(request, template_name="app/detal.html", context=context)

app/urls.py中如下

from django.conf.urls import url, include from . import views urlpatterns = [    url(r'^index/$', views.index, name="index"),    url(r'^(?P<question_id>[0-9]+)/$', views.detal, name="detal"),# name 指定名稱,用于后面的反向解析 ] ]

詳情頁html 配置如下

<!DOCTYPE html> <html> <head>    <meta charset="UTF-8">    <title>測試數(shù)據(jù)</title> </head> <body> {% if detal %} <h2>{{ detal.question_text }}</h2> {% for question in detal.choice_set.all %} <li>    {{ question.votes }}    {{ question.choice_text }} </li> {% endfor %} {% endif %} </body> </html>

index.html 修改如下

<!DOCTYPE html> <html> <head>    {% load static %}    <meta charset="UTF-8">    <title>測試數(shù)據(jù)</title> </head> <body> <img src="{% static  'django.jpg'%}"> {% if lastes_question_list %} <ul>    {% for question in lastes_question_list %}    <li>      <a href="{% url 'detal' question.id  %}"> {{question.question_text}} </a>    </li>    {% endfor %} </ul> {% endif%} </body> </html>

看完上述內(nèi)容,你們掌握django的環(huán)境配置和view的使用方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章名稱:django的環(huán)境配置和view的使用-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article36/dsgcsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站營銷、自適應網(wǎng)站、網(wǎng)站導航、面包屑導航品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司