91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

昌黎县| 江口县| 金寨县| 陆丰市| 绩溪县| 运城市| 府谷县| 佛山市| 昌乐县| 巨野县| 钦州市| 磴口县| 旬邑县| 余江县| 沙洋县| 禄丰县| 安泽县| 上饶市| 苍梧县| 西乌| 灵石县| 大竹县| 武山县| 鄱阳县| 民县| 汶上县| 曲水县| 曲麻莱县| 肇州县| 舞阳县| 防城港市| 尉犁县| 洛扎县| 惠州市| 衡南县| 高密市| 东辽县| 顺平县| 南江县| 虹口区| 乃东县|