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

django学习笔记(2)的更多相关文章

  1. Django 学习笔记之四 QuerySet常用方法

    QuerySet是一个可遍历结构,它本质上是一个给定的模型的对象列表,是有序的. 1.建立模型: 2.数据文件(test.txt) 3.文件数据入库(默认的sqlite3) 入库之前执行 数据库同步命 ...

  2. Django 学习笔记之三 数据库输入数据

    假设建立了django_blog项目,建立blog的app ,在models.py里面增加了Blog类,同步数据库,并且建立了对应的表.具体的参照Django 学习笔记之二的相关命令. 那么这篇主要介 ...

  3. Django学习笔记(五)—— 表单

    疯狂的暑假学习之  Django学习笔记(五)-- 表单 參考:<The Django Book> 第7章 1. HttpRequest对象的信息 request.path         ...

  4. Django学习笔记(三)—— 型号 model

    疯狂暑期学习 Django学习笔记(三)-- 型号 model 參考:<The Django Book> 第5章 1.setting.py 配置 DATABASES = { 'defaul ...

  5. Django 学习笔记(二)

    Django 第一个 Hello World 项目 经过上一篇的安装,我们已经拥有了Django 框架 1.选择项目默认存放的地址 默认地址是C:\Users\Lee,也就是进入cmd控制台的地址,创 ...

  6. Django 学习笔记(五)模板标签

    关于Django模板标签官方网址https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.IF标签 Hello World/vi ...

  7. Django 学习笔记(四)模板变量

    关于Django模板变量官方网址:https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.传入普通变量 在hello/Hell ...

  8. Django 学习笔记(三)模板导入

    本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...

  9. Django 学习笔记(七)数据库基本操作(增查改删)

    一.前期准备工作,创建数据库以及数据表,详情点击<Django 学习笔记(六)MySQL配置> 1.创建一个项目 2.创建一个应用 3.更改settings.py 4.更改models.p ...

  10. Django 学习笔记(六)MySQL配置

    环境:Ubuntu16.4 工具:Python3.5 一.安装MySQL数据库 终端命令: sudo apt-get install mysql-server sudo apt-get install ...

随机推荐

  1. 固定UIScrollView滑动的方向

    固定UIScrollView滑动的方向 一般而言,我们通过这两个参数CGRectMake以及contentSize就可以自动的让UIScrollView只往一个方向滚动.但我遇到过非常奇葩的情况,那就 ...

  2. 理解http请求

    HTTP请求的GET方法可以用来抓取网页. HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则,计算机专家设计出HTTP,使HTTP客户(如Web浏览 ...

  3. C# Socket编程 笔记,Socket 详解,入门简单

    目录 一,网络基础 二,Socket 对象 三,Bind() 绑定与 Connect() 连接 四,Listen() 监听请求连接 和 Accept() 接收连接请求 五,Receive() 与 Se ...

  4. lumen框架的辅助函数

    简介 Laravel 包含一些多样化的 PHP 辅助函数函数.许多在 Laravel 自身框架中使用:如果你觉得实用,也可以在你应用当中使用. 可用方法 数组 array_add array_coll ...

  5. Ubuntu 14.04 修改时区

    执行下面命令,并按照提示选择"Asia/Shanghai": sudo dpkg-reconfigure tzdata 正常执行结果为: Current default time ...

  6. Jinja2 简明使用手册

    @Jinja2 简明使用手册(转载) 介绍 Jinja是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity. 运行需求 Jinja2需要 ...

  7. 【转】iOS - SQLite 数据库存储

    本文目录 1.SQLite 数据库 2.iOS 自带 SQLite 的使用 3.fmdb 的使用 4.fmdb 多线程操作 5.其他 SQLite 的第三方封装库 回到顶部 1.SQLite 数据库 ...

  8. SVN提交的动作解释

    今天更新svn,发现有很多动作,其中字母代表的意思半知半解,就随手记录下来: A:add,新增 C:conflict,冲突 D:delete,删除 M:modify,本地已经修改 G:modify a ...

  9. 石头合并 NYOJ737 区间dp

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737 石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB ...

  10. FFmpeg中几个结构体的意义

    AVCodec是存储编解码器信息的结构体,特指一个特定的解码器,比如H264编码器的名字,ID,支持的视频格式,支持的采样率等: AVCodecContext是一个描述编解码器采用的具体参数,比如采用 ...