這篇文章主要介紹了django的環(huán)境配置和view的使用 ,具有一定借鑒價(jià)值,需要的朋友可以參考下。步驟簡單適合新手,希望你能收獲更多。下面是配置和使用的步驟內(nèi)容。
成都創(chuàng)新互聯(lián)是專業(yè)的武城網(wǎng)站建設(shè)公司,武城接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行武城網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
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ù)庫配置如下
基本時(shí)區(qū)和MySQL配置及相關(guān)時(shí)區(qū)配置請看django基礎(chǔ)
https://blog.51cto.com/11233559/2444627
啟動結(jié)果如下
django內(nèi)置了自己的模板引擎,和jinjia 很像,使用簡單
使用 Template 進(jìn)行定義模板,使用Context 將數(shù)據(jù)導(dǎo)入到該模板中,其導(dǎo)入默認(rèn)使用字典
django 默認(rèn)會去到app_name/templates下尋找模板,這是settings中的默認(rèn)設(shè)置,默認(rèn)會去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
python manage.py makemigrations python manage.py migrate
結(jié)果如下
創(chuàng)建后臺登陸用戶,設(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(""" {% if lastes_question_list %} {% for question in lastes_question_list %} - {{ question.question_text }}
{% endfor %}
{% endif %} """) context = Context({"lastes_question_list": lastes_question_list}) return HttpResponse(template.render(context))
訪問配置,結(jié)果如下
index 代碼如下
測試數(shù)據(jù) {% if lastes_question_list %} {% for question in lastes_question_list %} - {{question.question_text}}
{% endfor %}
{% endif%}
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ù)根路由中注冊的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[0-9]+)/$', views.detal, name="detal"),# name 指定名稱,用于后面的反向解析 ] ]
詳情頁html 配置如下
測試數(shù)據(jù) {% if detal %} {{ detal.question_text }}
{% for question in detal.choice_set.all %} {{ question.votes }} {{ question.choice_text }} {% endfor %} {% endif %}
index.html 修改如下
{% load static %} 測試數(shù)據(jù) {% if lastes_question_list %} {% for question in lastes_question_list %} - {{question.question_text}}
{% endfor %}
{% endif%}
看完上述內(nèi)容,你們掌握django的環(huán)境配置和view的使用方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!