温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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