django学习笔记(2)
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)的更多相关文章
- Django 学习笔记之四 QuerySet常用方法
QuerySet是一个可遍历结构,它本质上是一个给定的模型的对象列表,是有序的. 1.建立模型: 2.数据文件(test.txt) 3.文件数据入库(默认的sqlite3) 入库之前执行 数据库同步命 ...
- Django 学习笔记之三 数据库输入数据
假设建立了django_blog项目,建立blog的app ,在models.py里面增加了Blog类,同步数据库,并且建立了对应的表.具体的参照Django 学习笔记之二的相关命令. 那么这篇主要介 ...
- Django学习笔记(五)—— 表单
疯狂的暑假学习之 Django学习笔记(五)-- 表单 參考:<The Django Book> 第7章 1. HttpRequest对象的信息 request.path ...
- Django学习笔记(三)—— 型号 model
疯狂暑期学习 Django学习笔记(三)-- 型号 model 參考:<The Django Book> 第5章 1.setting.py 配置 DATABASES = { 'defaul ...
- Django 学习笔记(二)
Django 第一个 Hello World 项目 经过上一篇的安装,我们已经拥有了Django 框架 1.选择项目默认存放的地址 默认地址是C:\Users\Lee,也就是进入cmd控制台的地址,创 ...
- Django 学习笔记(五)模板标签
关于Django模板标签官方网址https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.IF标签 Hello World/vi ...
- Django 学习笔记(四)模板变量
关于Django模板变量官方网址:https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.传入普通变量 在hello/Hell ...
- Django 学习笔记(三)模板导入
本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...
- Django 学习笔记(七)数据库基本操作(增查改删)
一.前期准备工作,创建数据库以及数据表,详情点击<Django 学习笔记(六)MySQL配置> 1.创建一个项目 2.创建一个应用 3.更改settings.py 4.更改models.p ...
- Django 学习笔记(六)MySQL配置
环境:Ubuntu16.4 工具:Python3.5 一.安装MySQL数据库 终端命令: sudo apt-get install mysql-server sudo apt-get install ...
随机推荐
- 关于Vue中:key="index"的console警告
在写vue项目时,浏览器的console出现如下警告信息: [Vue warn]: Property or method "index" is not defined on the ...
- c# 托管和非托管的介绍
在.net 编程环境中,系统的资源分为托管资源和非托管资源. 对于托管的资源的回收工作,是不需要人工干预回收的,而且你也无法干预他们的回收,所能够做的 只是了解.net CLR如何做这些操作.也就是说 ...
- Forefront TMG 之 ISP 冗余传输链路(ISP-R)
在 Forefront TMG 中,新增了ISP 冗余传输链路功能:在 TMG 中,你可以同时使用两条活动的外部链路,使用模式分为以下两种: 故障转移模式:在主要链路工作正常的情况下,所有的流量都通过 ...
- Linux setfacl/getfacl命令详解
setfacl,命令名,设置文件访问控制列表,即ACL规则.而Acl(Access Control List)就是访问控制列表 setfacl常见命令参数 setfacl 2.2.51 -- 设定文件 ...
- windows server 2016 无法联网问题
首先,联网分解为两个问题,一.WLAN(无线网).二.以太网(有线网) 一 .WLAN问题解决方案 1.打开服务器管理器 2.添加角色和功能 3.一直点下一步到“功能”,勾选 DirectPlay 和 ...
- QQ邮箱验证码
人的记忆有时候跟鱼一样,只有七秒钟,短暂的时间! .NET Web窗体实现忘记密码,使用QQ邮箱验证修改 一.首先设置一下发送个人或企业发送的邮箱 二.登录邮箱进行设置,如图: 三.关闭邮箱 四. ...
- right here waiting的歌词
right here waiting的歌词 2006-12-30 17:36 匿名 | 分类:音乐 | 该问题已经合并到>> right here waiting的歌词有吗? 扫描二维 ...
- 面向对象程序设计_Task5_Calculator1.5.0
The 3rd part of the Calculator program _ FILE I/O 题目链接:第五次作业(计算器第三步) github链接:Calculator_1.5.0 第五次作业 ...
- Django之Model (ORM)
传统操作数据库 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层 ...
- CORS跨域模型浅析及常见理解误区分析
CORS跨域资源共享是前后端跨域十分常用的一种方案,主要依赖Access-Control-Allow(ACA)系列header来实现一种协商性的跨域交互. 基本模型 其中的具体流程大致可以分为以下几步 ...