程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pythons Django framework (2)

編輯:Python

本篇主要講解Django框架的admin站點管理.

1. admin站點管理

django本身提供了admin站點管理應用,在每一個app提供了admin.py腳本文件,可以將當前應用的models.py中模型類,添加到admin站點中,以方便管理員管理模型對應的數據.

1.1 創建管理員賬號(超級管理員賬號)

python manage.py createsuperuser

如上圖所示,設置的用戶名,郵箱地址,密碼均為:[email protected]

接下來,運行程序

python manage.py runserver

輸入:http://127.0.0.1:8000/admin/ 回車之後,跳轉頁面

1.1添加用戶

成功添加用戶信息,用戶名為admin,密碼為[email protected](注意,This is not the same as the super administrator account)

At this point, pay attention to the state in the figure below

退出登錄狀態,重新登錄

Then log in with the newly added user,報如下錯誤

解決辦法:

Log in with the super administrator account,Enter the area to modify the user's status

Add to this userStaff status

The operation is as follows

Log in again with this account to test

At this point in the database,The following tables are more important

總結:

啟動服務之後,可以訪問/admin進入站點後台管理頁面.

可以嘗試創建後台管理人員賬號,將activate和staff status勾選上.並添加login應用的管理User模型的權限.

1.2 將login應用中的模型類添加到admin.py中

在login應用的admin.py文件添加代碼如下:

from django.contrib import admin
from .models import User
# Register your models here.
admin.site.register(User)

保存之後,Refresh the site page

If there will be four pieces of data,Check the database to know,Four pieces of data are saved

1.3添加用戶信息

此時修改一下代碼,使user_id可以為空

user_id = models.IntegerField(primary_key=True,blank=True)

下面添加用戶

Now look at the database data,多了一條

1.4 Added user interface settings in Chinese

修改login文件裡面的models.py代碼

保存之後,刷新admin頁面

1.5 修改ADD user 中文

在login模塊models.py添加代碼

 class Meta:
verbose_name = '會員'
verbose_name_plural = verbose_name

效果如下

1.6 修改語言和時區

LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'

Refresh the site page,效果如下:

1.6 Modify the columns of the site

在login的admin.py修改代碼如下:

from django.contrib import admin
from .models import User
# Register your models here.
class UserAdmin(admin.ModelAdmin):
list_display = ('user_id','name','phone','email')
list_display_links = ('name',)
# 過濾器
list_filter = ('name','phone')
list_editable = ('phone','email')
search_fields = ('name','phone','email')
admin.site.register(User,UserAdmin)

1.7 修改模型類

注意添加Field字段參數的blank和verbase_name

The above code has been modified to report the following error,Take a look at the image below.

保存刷新,界面如下

總結

There are two ways to write the site

方法一:
class UserAdmin(admin.ModelAdmin):
list_display = ('user_id','name','phone','email')
list_display_links = ('name',)
# 過濾器
list_filter = ('name','phone')
list_editable = ('phone','email')
search_fields = ('name','phone','email')
admin.site.register(User,UserAdmin)
方法二
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
list_display = ('user_id','name','phone','email')
list_display_links = ('name',)
# 過濾器
list_filter = ('name','phone')
list_editable = ('phone','email')
search_fields = ('name','phone','email')


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved