Django入門筆記 二

2022-09-15 00:12:34 字數 3884 閱讀 3979

入門筆記翻譯整理自:

1. 建立管理員(admin user)

執行**

1

$ python manage.py createsuperuser23

username: admin

4email address: [email protected]

56 password: *********

7 password(again): *********

8 superuser created successfully.

2. 啟動開發伺服器(development server)

啟動伺服器

1 $ python manage.py runserver

開啟本地網域名稱,比如瀏覽器將顯示管理員登陸頁面。

3. 進入管理員頁面(admin site)

輸入使用者名稱和密碼

4. 使投票應用可以被管理員更改

投票應用顯示在管理員頁面

1

#polls/admin.py23

from django.contrib import

admin45

from .models import

question

67 admin.site.register(question)

5. 探索管理員功能

略6. 自定義管理員表單(admin form)

對admin.site.register(question)進行替換

1

#polls/admin.py23

from django.contrib import

admin45

from .models import

question67

class

questionadmin(admin.modeladmin):

8 fields = ['

pub_date

', '

question_text']

910 admin.site.register(question, questionadmin)

基本的思路是:建立乙個modeladmin物件,然後將它作為第二個引數傳遞到register()方法中

再比如,

1

#polls/admin.py23

from django.contrib import

admin45

from .models import

question67

class

questionadmin(admin.modeladmin):

8 fieldsets =[

9 (none, ),

10 ('

date information

', ), 11]

1213 admin.site.register(question, questionadmin)

7. 新增關聯物件(related objects)

在投票應用中,question有關聯物件choice。有兩種方式新增關聯物件

1

#polls/admin.py23

from django.contrib import

admin45

from .models import

choice, question6#

...7 admin.site.register(choice)

還可以

1

#polls/admin.py23

from django.contrib import

admin45

from .models import

choice, question67

class

choiceinline(admin.stackedinline):

8 model =choice

9 extra = 3

1011

class

questionadmin(admin.modeladmin):

12 fieldsets =[

13 (none, ),

14 ('

date information

',),15]

16 inlines =[choiceinline]

1718 admin.site.register(question, questionadmin)

此外,以下**可以讓介面簡潔

1

#polls/admin.py23

class

choiceinline(admin.tabularinline):4#

...

8. 自定義更改列表(the admin change list)

執行**

1

#polls/admin.py23

class

questionadmin(admin.modeladmin):4#

...5 list_display = ('

question_text

', '

pub_date

', '

was_published_recently

')

對於自定義方法was_published_recently,排序在預設狀態下不能實現,且列表名稱為方法名稱,可以通過以下方式改進

1

#polls/models.py23

class

question(models.model):4#

...5

defwas_published_recently(self):

6return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

7 was_published_recently.admin_order_field = '

pub_date

'8 was_published_recently.boolean =true

9 was_published_recently.short_description = '

published recently?

'

可以向questionadmin物件新增過濾器和搜尋功能

1

#polls/admin.py23

class

questionadmin(admin.modeladmin):4#

...5 list_filter = ['

pub_date

']6 search_fields = ['question_text

']

9. 自定義管理頁面外觀(admin look and feel)

在manage.py所在目錄下,建立templates子目錄。新增dirs到設定檔案(mysite/settings.py,牢記)中的templates設定:

1 templates =[2,

14},

15 ]

在templates目錄中,建立admin子目錄,並複製django/contrib/admin/templates中的模板admin/base_site.html。然後對base_site.html進行修改。

步驟和9類似

--the end--

django入門筆記

2,檢視django是否安裝 python m django version 3,建立專案 django admin startproject myblog manage.py 與專案進行互動的命令列工具集的入口 wsgi.py wsgi python web server gateway inte...

Django學習筆記 快速入門

基於以下環境 windows 10 python 3.6 django 1.18生成成功後,可以看到如下專案結構 mysite manage.py mysite init py settings.py urls.py wsgi.py 示例 django.contrib.admin django.co...

Django學習筆記 快速入門

基於以下環境 windows 10 python 3.6 django 1.18生成成功後,可以看到如下專案結構 mysite manage.py mysite init py settings.py urls.py wsgi.py 示例 django.contrib.admin django.co...