您好,登錄后才能下訂單哦!
這篇文章主要介紹了django的環(huán)境配置和view的使用 ,具有一定借鑒價(jià)值,需要的朋友可以參考下。步驟簡(jiǎn)單適合新手,希望你能收獲更多。下面是配置和使用的步驟內(nèi)容。
mkdir djanad cd djanad/ pyenv virtualenv 3.6.5 djanad pyenv local djanad
結(jié)果如下
pip install django==2.1
django-admin startproject demo . django-admin startapp app
結(jié)果如下
數(shù)據(jù)庫(kù)配置如下
基本時(shí)區(qū)和mysql配置及相關(guān)時(shí)區(qū)配置請(qǐng)看django基礎(chǔ)
https://blog.51cto.com/11233559/2444627
啟動(dòng)結(jié)果如下
django內(nèi)置了自己的模板引擎,和jinjia 很像,使用簡(jiǎn)單
使用 Template 進(jìn)行定義模板,使用Context 將數(shù)據(jù)導(dǎo)入到該模板中,其導(dǎo)入默認(rèn)使用字典
django 默認(rèn)會(huì)去到app_name/templates下尋找模板,這是settings中的默認(rèn)設(shè)置,默認(rèn)會(huì)去app_name/static找那個(gè)尋找靜態(tài)文件(css,js,jpg,html)等
在 app/models.py 中創(chuàng)建數(shù)據(jù)庫(kù)表模板,具體配置如下:
from django.db import models # Create your models here. # 問(wèn)題 class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __str__(self): return self.question_text # 選擇 # 配置選擇為問(wèn)題的外鍵,并配置選擇的內(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
python manage.py makemigrations python manage.py migrate
結(jié)果如下
創(chuàng)建后臺(tái)登陸用戶,設(shè)置用戶名為admin,密碼為admin@123
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/
demo/setting.py 中配置添加
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
項(xiàng)目中創(chuàng)建static 并上傳圖片django.jpg
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) ]
from django.conf.urls import url, include from . import views urlpatterns = [ url(r'^index/$', views.index, name="index"), # name 指定名稱, ]
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))
訪問(wèn)配置,結(jié)果如下
index 代碼如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>測(cè)試數(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))
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)
根據(jù)根路由中注冊(cè)的namespace和子路由中注冊(cè)的name來(lái)動(dòng)態(tài)獲取路徑。在模板中使用"{% url namespace:name %}"
如果攜帶位置參數(shù)
“{% url namespace:name args %}"
如果攜帶關(guān)鍵字參數(shù)
“{% url namespace:name k1=v1 k2=v2 %}"
配置 詳情頁(yè)面添加數(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 指定名稱,用于后面的反向解析 ] ]
詳情頁(yè)html 配置如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>測(cè)試數(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>測(cè)試數(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的使用方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。