Django筆記
來做官方文件的內容翻譯篩選
1、django-admin startproject mysite
2、Python manage.py
startapp polls
3、編寫app裡面的models.py檔案
import datetime
from django.db import models
from django.utils import timezone
# 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
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
4、python manage.py
makemigrations
,為這些修改建立遷移檔案
5、python manage.py migrate
,將這些改變更新到資料庫中。
6、python manage.py createsuperuser
7、修改polls/admin.py讓poll應用在管理站點中可編輯
from django.contrib import admin
from .models import Question
admin.site.register(Question)
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
8、建立一個模型管理物件(class),然後把該物件(class名)作為第二個引數傳入admin.site.register(),來自定義管理表單
from django.contrib import admin
# Register your models here.
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fields = ['pub_date','question_text']
admin.site.register(Question,QuestionAdmin)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
9、把表單分割成欄位集,fieldsets中每個元組的第一個元素是欄位集的標題
from django.contrib import admin
# Register your models here.
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
# fields = ['pub_date','question_text']
fieldsets = [
(None, {'fields':['question_text']}),
('時間資訊',{'fields':['pub_date']}),
]
admin.site.register(Question,QuestionAdmin)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
10、建立Question物件的同時可以直接新增一組Choice
from django.contrib import admin
# Register your models here.
from .models import Question,Choice
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
# fields = ['pub_date','question_text']
fieldsets = [
(None, {'fields':['question_text']}),
('時間資訊',{'fields':['pub_date']}),
]
inlines = [ChoiceInline]
admin.site.register(Question,QuestionAdmin)
# admin.site.register(Choice)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
這告訴Django:Choice物件在Question的管理介面中編輯。預設提供足夠3個Choice的空間
11、預設地,Django顯示每個物件的str()返回的內容。但有時如果我們能顯示個別的欄位將很有幫助。 我們使用list_display 選項來實現這個功能
from django.contrib import admin
# Register your models here.
from .models import Question,Choice
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
# fields = ['pub_date','question_text']
fieldsets = [
(None, {'fields':['question_text']}),
('時間資訊',{'fields':['pub_date']}),
]
inlines = [ChoiceInline]
list_display = ('question_text','pub_date','was_published_recently')
admin.site.register(Question,QuestionAdmin)
# admin.site.register(Choice)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
12、新增過濾器和搜尋功能
from django.contrib import admin
# Register your models here.
from .models import Question,Choice
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
# fields = ['pub_date','question_text']
fieldsets = [
(None, {'fields':['question_text']}),
('時間資訊',{'fields':['pub_date']}),
]
inlines = [ChoiceInline]
list_display = ('question_text','pub_date','was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_text']
admin.site.register(Question,QuestionAdmin)
# admin.site.register(Choice)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
13、自定義管理介面模版
14、修改檢視函式,修改urlconf,把app裡面的urls.py包含進去
https://django-intro-zh.readthedocs.io/zh_CN/latest/part3/
# polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail,name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results,name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote,name='vote'),
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
15、載入 polls/index.html 模板檔案,並且向它傳遞一個上下文環境(context)
# polls/views.py
from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
16.快捷函式render
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from .models import Question
from django.template import loader
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
# output = ','.join([p.question_text for p in latest_question_list])
# template = loader.get_template('polls/index.html')
context = {'latest_question_list':latest_question_list}
return render(request,'polls/index.html',context)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
17.丟擲 404 錯誤
# polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
18.快捷函式:get_object_of_404()
# polls/views.py
from django.shortcuts import get_object_or_404, render
from .models import Question
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
19.去除模板中的硬編碼 URL,在 polls.urls 的 urls() 函式中通過 name 引數為 URL 定義了名字,你可以使用 {% url %} 標籤代替它
20.為 URL 名稱新增名稱空間
# mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
現在,編輯 polls/index.html 檔案,從:
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
修改成:
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
20.編寫檢視函式,可以精簡刪除舊的 index、detail和 results 檢視,並用 Django 的通用檢視代替(第四部分)
21.編寫表單做測試
22.有人要原始碼麼?原始碼:http://pan.baidu.com/s/1bzJfzC
有學Django的一起呀!
中文翻譯1:http://python.usyiyi.cn/documents/django_182/intro/tutorial02.html
中文翻譯2:https://django-intro-zh.readthedocs.io/zh_CN/latest/whats_next/
相關文章
- Django 筆記Django筆記
- django基礎筆記Django筆記
- django學習筆記Django筆記
- django專案筆記1Django筆記
- Django 筆記 - 特殊運算子Django筆記
- Django筆記四十一之Django中使用esDjango筆記
- Django Book 學習筆記(上)Django筆記
- Django Book學習筆記(下)Django筆記
- Django 筆記 - 特殊運算子 2Django筆記
- django下載csv檔案筆記Django筆記
- Django筆記三十四之分頁操作Django筆記
- [python]django學習筆記 二PythonDjango筆記
- Django學習筆記—驗證碼Django筆記
- Django學習筆記(12)——分頁功能Django筆記
- Django學習筆記(15)——中介軟體Django筆記
- Django restframework 框架筆記 (二) 許可權DjangoRESTFramework框架筆記
- 不一樣的django2.0筆記Django筆記
- Django 上下文處理器 筆記Django筆記
- Django筆記十九之manager用法介紹Django筆記
- Django筆記三十三之快取操作Django筆記快取
- Django book2 模型 學習筆記Django模型筆記
- Django筆記三十之log日誌記錄詳解Django筆記
- Django筆記四十之執行Django環境的python指令碼Django筆記Python指令碼
- Django筆記三十八之傳送郵件Django筆記
- Django筆記十六之aggregate聚合操作Django筆記
- Docker筆記三之執行Django系統Docker筆記Django
- django book2 表單學習筆記Django筆記
- Django 原始碼閱讀筆記(基礎檢視)Django原始碼筆記
- Django學習筆記《二》圖書管理系統Django筆記
- Django筆記十二之defer、only指定返回欄位Django筆記
- Django筆記十七之group by 分組用法總結Django筆記
- Django筆記二十二之多資料庫操作Django筆記資料庫
- Django之“學習筆記”網站開發1Django筆記網站
- Django學習筆記—Comments庫的使用方法小記Django筆記
- Django筆記四十四之Nginx+uWSGI部署Django以及Nginx負載均衡操作Django筆記Nginx負載
- (Django)18.3建立網頁:學習筆記主頁Django網頁筆記
- 關於django reset_framework學習之路的筆記DjangoFramework筆記
- Django筆記二十之手動編寫migration檔案Django筆記