亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

100django_admin

發布時間:2020-06-24 12:59:35 來源:網絡 閱讀:300 作者:chaijowin 欄目:編程語言

?

?

admin

?

requirement

django強大的功能之一,提供了admin后臺管理界面,簡單配置就可對數據庫內容作管理;

?

mysite/mysite/settings.py

INSTALLED_APPS = [

??? # 'users.apps.UsersConfig',

??? 'blog.apps.BlogConfig',

??? 'publish.apps.PublishConfig',

??? 'bootstrap3',

??? 'books.apps.BooksConfig',

??? 'polls.apps.PollsConfig',

??? 'django.contrib.admin',

??? 'django.contrib.auth',?? #依賴1

??? 'django.contrib.contenttypes',?? #依賴2

??? 'django.contrib.sessions',?? #依賴3

? ??'django.contrib.messages',?? #依賴4

??? 'django.contrib.staticfiles',

]

TEMPLATES = [

??? {

??????? 'BACKEND': 'django.template.backends.django.DjangoTemplates',

??????? # 'BACKEND': 'django.template.backends.jinja2.Jinja2',

??????? 'DIRS': [os.path.join(BASE_DIR, 'templates')],

??????? 'APP_DIRS': True,

??????? 'OPTIONS': {

??????????? 'context_processors': [

??????????????? 'django.template.context_processors.debug',

??????????????? 'django.template.context_processors.request',

??????????????? 'django.contrib.auth.context_processors.auth',

??????????????? 'django.contrib.messages.context_processors.messages',

??????????? ],

??????? },

??? },

]

MIDDLEWARE = [

??? 'django.middleware.security.SecurityMiddleware',

??? 'django.contrib.sessions.middleware.SessionMiddleware',

??? 'django.middleware.common.CommonMiddleware',

??? 'django.middleware.csrf.CsrfViewMiddleware',

??? 'django.contrib.auth.middleware.AuthenticationMiddleware',

??? 'django.contrib.messages.middleware.MessageMiddleware',

??? 'django.middleware.clickjacking.XFrameOptionsMiddleware',

]

?

?

mysite/mysite/urls.py

urlpatterns = [

??? # url(r'^$', index),

??? # url(r'^hello/$', hello),

??? # url(r'^polls/', include('polls.urls')),

??? # url(r'^polls/', include('polls.urls', app_name='polls')),

??? # url(r'^polls/', include('polls.urls', namespace='polls')),

??? # url(r'^books/', include('books.urls')),

??? # url(r'^sendmail/', include('sendmail.urls')),

??? # url(r'publish/', include('publish.urls')),

??? url(r'blog/', include('blog.urls')),

??? url(r'^admin/', admin.site.urls),

]

?

以上是前4步;

5${app_dir}/admin.py,給每個模型創建一個ModelAdmin類,封裝模型自定義的Admin功能和選項;

6、注冊ModelAdmin

到此,可通過綁定的url來訪問管理站點,默認是/admin/

?

注:

默認,is_superuseris_staff雙重身份才能在admin中進行管理和修改;

當只有is_staff,能進入后臺但不能進行任何操作;

當只有is_superuser,不能進入admin

>>> from django.contrib.auth.models import User

>>> User.objects.all()

<QuerySet []>

>>> u = User.objects.create_user(username='jowin',email='jowin@ane56.com')

>>> u.set_password('jowin')

>>> u.save()

>>> u

<User: jowin>

>>> u.is_superuser

False

>>> u.is_superuser=True

>>> u.is_staff

False

>>> u.is_staff=True

>>> u.save()

>>> u.is_active

True

>>> quit()

?

?

配置ModelAdmin

?

django默認會到${app_name}下找admin.py模塊;

自動根據modelfield類型設置form類型;

?

label名稱,定義model field時指定verbose_name

exclude,排除某字段,不展示;

fields,顯示某字段;

date_hierarchy,添加日期標簽過濾;

ordering,排序;

search_fields,搜索某列;

list_display,列表顯示更多列;

list_filter

100django_admin

?

mysite/blog/admin.py

from django.contrib import admin

from .models import Blog, Author, Entry

?

# admin.site.register(Blog)?? #方式1

# admin.site.register(Author)

# admin.site.register(Entry)

?

@admin.register(Blog)?? #方式2

class BlogAdmin(admin.ModelAdmin):

??? pass

?

@admin.register(Entry)

class EntryAdmin(admin.ModelAdmin):

??? pass

?

@admin.register(Author)

class AuthorAdmin(admin.ModelAdmin):

??? pass

100django_admin

?

?

def authors_display(obj):

??? return ', '.join([ author.name for author in obj.authors.all()])

?

authors_display.short_description = 'Authors'

?

def make_pub_date_to_now(modeladmin, request, queryset):

??? queryset.update(pub_date=timezone.now())

?

make_pub_date_to_now.short_description = '批量修改發布日期為今天'

?

@admin.register(Entry)

class EntryAdmin(admin.ModelAdmin):

??? exclude = ('n_comments', 'n_pingbacks')

??? date_hierarchy = 'pub_date'

??? search_fields = ('headline', 'body_text')

??? list_display = ('headline', 'blog', 'pub_date', authors_display)

??? actions = [make_pub_date_to_now]

100django_admin

注:

web頁上labelform label),對應models.py中的verbose_name

?

100django_admin

100django_admin

注:

與代碼中authors_display.short_description = 'Authors'對應;

?

100django_admin

注:

Actions與代碼中actions = [make_pub_date_to_now]對應;

?

?

?


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

班玛县| 黄浦区| 玉林市| 泊头市| 循化| 望奎县| 大渡口区| 榆林市| 河源市| 东台市| 墨玉县| 普兰县| 宝应县| 应城市| 五峰| 阜城县| 勃利县| 上林县| 犍为县| 疏附县| 池州市| 张家港市| 福清市| 乐陵市| 东山县| 株洲县| 长宁区| 阿勒泰市| 武邑县| 白水县| 昌宁县| 汶上县| 乌拉特前旗| 那曲县| 松滋市| 大港区| 福泉市| 北宁市| 曲松县| 汾阳市| 随州市|