django学习笔记(2)

简介: Part 2: The admin site ====> Creating an admin user$ python manage.py createsuperuser   Username: admin   Email address: admin@example.

Part 2: The admin site

====> Creating an admin user
$ python manage.py createsuperuser
   Username: admin
   Email address: admin@example.com
   Password: **********
   Password (again): *********
   Superuser created successfully.


====> Start the development server
$ python manage.py runserver



====> Enter the admin site
(http://127.0.0.1:8000/admin/)


====> Make the poll app modifiable in the admin
$ edit polls\admin.py

from django.contrib import admin

from .models import Question

admin.site.register(Question)



====> Explore the free admin functionality



====> Customize the admin form
$ edit polls\admin.py

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question_text']

admin.site.register(Question, QuestionAdmin)





====> Split the form up into fieldsets
$ edit polls\admin.py

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

admin.site.register(Question, QuestionAdmin)




====> Assign arbitrary HTML classes to each fieldset
$ edit polls\admin.py

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]

admin.site.register(Question, QuestionAdmin)



====> Adding related objects
$ edit polls\admin.py

from django.contrib import admin

from .models import Choice, Question
# ...
admin.site.register(Choice)


====> Remove the register() call for the Choice model. Then, change the Question registration code
$ edit polls\admin.py

from django.contrib import admin

from .models import Choice, Question


class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

admin.site.register(Question, QuestionAdmin)




====> Change the display style of the related objects in a more compact, table-based format
$ edit polls\admin.py

class ChoiceInline(admin.TabularInline):
    #...



====> Customize the admin change list -- use the list_display admin option
$ edit polls\admin.py

class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date', 'was_published_recently')



====> Add a “Filter” sidebar -- using the list_filter admin option
$ edit polls\admin.py

class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_filter = ['pub_date']


====> Add some search capability
$ edit polls\admin.py

class QuestionAdmin(admin.ModelAdmin):
    # ...
    search_fields = ['question_text']





====> Customize the admin look and feel

====> Customizing your project’s templates
$ mkdir templates
$ edit mysite\settings.py
TEMPLATES = [
    {
        # ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        # ...
    },
]
$ mkdir templates\admin
$ copy C:\python34\lib\site-p~1\django\contrib\admin\templates\amdin\base_site.html templates\admin\base_site.html
$ edit tempaltes\admin\base_site.html
# replace {{ site_header|default:_('Django administration') }} with Polls Administration
# ...
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>
{% endblock %}
# ...




====> Customizing your application’s templates



====> Customize the admin index page


目录
相关文章
|
7月前
|
Python
基于Django的Python应用—学习笔记—功能完善
基于Django的Python应用—学习笔记—功能完善
|
7月前
|
存储 数据库 数据安全/隐私保护
基于Django的Python应用——学习笔记
基于Django的Python应用——学习笔记
|
安全 中间件 开发者
【Django学习笔记 - 9】:装饰器在类视图中的使用、中间件(开发中间件)、知识点小补充(子应用的集中管理)
【Django学习笔记 - 9】:装饰器在类视图中的使用、中间件(开发中间件)、知识点小补充(子应用的集中管理)
400 6
【Django学习笔记 - 9】:装饰器在类视图中的使用、中间件(开发中间件)、知识点小补充(子应用的集中管理)
|
数据库 数据安全/隐私保护 Python
【Django学习笔记 - 19】:认证、自定义认证、权限、限流
【Django学习笔记 - 19】:认证、自定义认证、权限、限流
289 0
【Django学习笔记 - 19】:认证、自定义认证、权限、限流
|
缓存 前端开发 数据库
【Django学习笔记 - 18】:drf请求响应简介、基类(APIView、GenericAPIView)、mixin扩展类与三级视图、视图集与路由2
【Django学习笔记 - 18】:drf请求响应简介、基类(APIView、GenericAPIView)、mixin扩展类与三级视图、视图集与路由
169 0
【Django学习笔记 - 18】:drf请求响应简介、基类(APIView、GenericAPIView)、mixin扩展类与三级视图、视图集与路由2
|
XML JSON 前端开发
【Django学习笔记 - 18】:drf请求响应简介、基类(APIView、GenericAPIView)、mixin扩展类与三级视图、视图集与路由
【Django学习笔记 - 18】:drf请求响应简介、基类(APIView、GenericAPIView)、mixin扩展类与三级视图、视图集与路由
213 0
【Django学习笔记 - 18】:drf请求响应简介、基类(APIView、GenericAPIView)、mixin扩展类与三级视图、视图集与路由
|
XML JSON 前端开发
【Django学习笔记 - 17】:序列化和反序列化(restful接口小案例、DRF的工程搭建、序列化器与序列化、验证、保存)
【Django学习笔记 - 17】:序列化和反序列化(restful接口小案例、DRF的工程搭建、序列化器与序列化、验证、保存)
536 0
【Django学习笔记 - 17】:序列化和反序列化(restful接口小案例、DRF的工程搭建、序列化器与序列化、验证、保存)
|
JSON 前端开发 JavaScript
【Django学习笔记 - 16】:DRF概述、Web应用模式(前后端分离简介)
【Django学习笔记 - 16】:DRF概述、Web应用模式(前后端分离简介)
307 0
【Django学习笔记 - 16】:DRF概述、Web应用模式(前后端分离简介)
|
Python
【Django学习笔记 - 15】:admin站点编辑(关联对象在列表页中添加,编辑页调整、图片设置)2
【Django学习笔记 - 15】:admin站点编辑(关联对象在列表页中添加,编辑页调整、图片设置)
105 0
【Django学习笔记 - 15】:admin站点编辑(关联对象在列表页中添加,编辑页调整、图片设置)2