您好,登錄后才能下訂單哦!
這篇文章主要介紹了django的環境配置和view的使用 ,具有一定借鑒價值,需要的朋友可以參考下。步驟簡單適合新手,希望你能收獲更多。下面是配置和使用的步驟內容。
mkdir djanad cd djanad/ pyenv virtualenv 3.6.5 djanad pyenv local djanad
結果如下
pip install django==2.1
django-admin startproject demo . django-admin startapp app
結果如下
數據庫配置如下
基本時區和mysql配置及相關時區配置請看django基礎
https://blog.51cto.com/11233559/2444627
啟動結果如下
django內置了自己的模板引擎,和jinjia 很像,使用簡單
使用 Template 進行定義模板,使用Context 將數據導入到該模板中,其導入默認使用字典
django 默認會去到app_name/templates下尋找模板,這是settings中的默認設置,默認會去app_name/static找那個尋找靜態文件(css,js,jpg,html)等
在 app/models.py 中創建數據庫表模板,具體配置如下:
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 # 選擇 # 配置選擇為問題的外鍵,并配置選擇的內容和選擇的起始值 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
結果如下
創建后臺登陸用戶,設置用戶名為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') ]
項目中創建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")), #此處配置名稱空間,用于處理后面的翻轉 ]
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))
訪問配置,結果如下
index 代碼如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>測試數據</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)
根據根路由中注冊的namespace和子路由中注冊的name來動態獲取路徑。在模板中使用"{% url namespace:name %}"
如果攜帶位置參數
“{% url namespace:name args %}"
如果攜帶關鍵字參數
“{% url namespace:name k1=v1 k2=v2 %}"
配置 詳情頁面添加數據
app/view.py 中添加數據如下
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>測試數據</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>測試數據</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>
看完上述內容,你們掌握django的環境配置和view的使用方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。