Django筆記
基礎(chǔ)部分
結(jié)構(gòu):
- URLs -->views --> models --> Templates
- URLs的參數(shù)<?P< name >\d+>可以傳入views和Templates
創(chuàng)建項(xiàng)目:
- django-admin startproject project_name
創(chuàng)建數(shù)據(jù)庫(kù)
創(chuàng)建模型:
- python manage.py startapp app_name
激活模型(settings.py):
- INSTALLED_APPS添加app_name
- python manage.py makemigrations app_name
- python manage.py sql app_name
- python manage.py syncdb
返回SQL語(yǔ)句:
- python manage.py sqlmigrate app_name 0001
創(chuàng)建一個(gè)管理員用戶
- python manage.py createsuperuser
廣播
- python manage.py runserver 127.0.0.1:8080
中級(jí)部分
models.py的設(shè)置:
- models.ForeignKey(類名)設(shè)置外鍵
views.py的設(shè)置:
a = app_classname.objects.all() 返回app_classname的所有項(xiàng)目(添加app_classname的__str__方法才能返回a的實(shí)際值)
app_classname.objects.get(id或者pk)
app_classname.objects.filter()
{ 當(dāng)類型為CharField的時(shí)候憎乙,有_startwith的方法
為DateTimeField的時(shí)候黍翎,有_year等方法 }
- a.id
- a.classdefname 對(duì)于外鍵的,有all(),create(),count()方法
admin.py設(shè)置:
- 使app在管理站點(diǎn)中可編輯: admin.site.register(app_classname)
- 高級(jí):
(類)表單管理:
class ChoceInline(admin.StackedInline):
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', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_text']
admin.site.register(Question, QuestionAdmin)
```
#### URL:
* from django.conf.urls import url url(regex, views,kwargs, name)
* 主URL:
url(r'^$', views.class_name, name)
* app_url:
url(r'^app/', include('app.urls'))
#### HTML:
* url中:blog/article/(?P<article_id>\d+)$ 則{% url 'blog:detail' article.id %}
* {% url '主url_name:次url_name' 傳入?yún)?shù) %}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者