重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

100django_admin

 

为中阳等地区用户提供了全套网页设计制作服务,及中阳网站建设行业解决方案。主营业务为成都网站建设、网站设计、中阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

 

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_superuser和is_staff双重身份才能在admin中进行管理和修改;

当只有is_staff,能进入后台但不能进行任何操作;

当只有is_superuser,不能进入admin;

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

>>> User.objects.all()

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

>>> u.set_password('jowin')

>>> u.save()

>>> u

>>> 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模块;

自动根据model的field类型设置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页上label(form label),对应models.py中的verbose_name;

 

100django_admin

100django_admin

注:

与代码中authors_display.short_description = 'Authors'对应;

 

100django_admin

注:

Actions与代码中actions = [make_pub_date_to_now]对应;

 

 

 


网站题目:100django_admin
网站网址:http://cqcxhl.cn/article/poeigi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP