网站建设资讯

NEWS

网站建设资讯

100django_admin-创新互联

成都创新互联主要从事网站设计制作、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务千山,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

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]对应;

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:100django_admin-创新互联
本文来源:http://cdweb.net/article/codshe.html