django admin自定义
自定义 Django admin
核心观点
- UI是很重要的。
- 考虑初学者,DashBoard。
- 导航。
- 用户工作流。
- Django admin 缺少 WYSIWYG,文件管理,undo。
建议
- 了解用户的行业和工作流。
- 了解客户团队中的各个不同角色。
- 使用客户习语。
- 了解之前使用的工具,及其优缺点。
实现
ModelAdmin Media
class ArticleAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("my_styles.css",)
}
js = ("my_code.js",)
优点:对于“一次性”项目简单。
缺点:只对Change Form有效。
Custom Templates
关键模板
admin/base.html
admin/index.html
admin/change_form.html
admin/change_list.html
app_index.html
delete_confirmation.html
覆盖范围
Across an entire project
admin/change_form.html
Across an application
admin/<my_app>/change_form.html
For an individual model
admin/<my_app>/<my_model>/change_form.html
例如:
{% extends "admin/change_list.html" %}
{% block object-tools %}
<h1 class="errornote">
Look Here!
</h1>
{{ block.super }}
{% endblock %}
自定义模板的建议:
Extend, don't override
Use {{ block.super }} to extend blocks
Extend a symlink of the admin templates in the event of recursion
Extend the extrahead block in base.html for admin-wide media
已有项目:
http://code.google.com/p/sorl-curator/
http://code.google.com/p/django-grappelli/
ModelAdmin/ModelForm Hacking
注册ModelAdmin
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from demo_app.models import UserProfile
class UserProfileInline(admin.TabularInline):
model = UserProfile
fk_name = 'user'
max_num = 1
class CustomUserAdmin(UserAdmin):
inlines = [UserProfileInline, ]
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
行级别的权限
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form,
change):
obj.user = request.user
obj.save()
def queryset(self, request):
qs = self.model._default_manager.filter(user=request.user)
return qs
ModelForm
class AuthorForm(forms.ModelForm):
exclude_states = ['AS', 'GU', 'MP', 'VI',]
def __init__(self, *args, **kwargs):
super(AuthorForm, self).__init__(*args,
**kwargs)
w = self.fields['state'].widget
choices = []
for key, value in w.choices:
if key not in self.exclude_states:
choices.append((key, value))
w.choices = choices
class AuthorAdmin(admin.ModelAdmin):
form = AuthorForm
Custom Views
Custom View URL
class PostAdmin(admin.ModelAdmin):
def my_view(self, request):
return admin_my_view(request, self)
def get_urls(self):
urls = super(PostAdmin, self).get_urls()
my_urls = patterns('',
(r'^my_view/$', self.my_view)
)
return my_urls + urls
Custom View
@permission_required('blog.add_post')
def admin_my_view(request, model_admin):
opts = model_admin.model._meta
admin_site = model_admin.admin_site
has_perm = request.user.has_perm(opts.app_label /
+ '.' + opts.get_change_permission())
context = {'admin_site': admin_site.name,
'title': "My Custom View",
'opts': opts,
'root_path': '/%s' % admin_site.root_path,
'app_label': opts.app_label,
'has_change_permission': has_perm}
template = 'admin/demo_app/admin_my_view.html'
return render_to_response(template, context,
context_instance=RequestContext(request))
Custom View Template
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="../../../">{% trans "Home" %}</a> ›
<a href="../../">{{ app_label|capfirst|
escape }}</a> ›
{% if has_change_permission %}<a
href="../">{{ opts.verbose_name_plural|
capfirst }}</a>{% else %}{{ opts.verbose_name_plural|
capfirst }}{% endif %} › My Custom View
</div>
{% endblock %}
{% block content %}
<!-- do stuff here -->
{% endblock %}
Django的内置模板
base.html
<head>
<title> {% block title %}{% endblock %} </title>
{% block extrastyle %}{% endblock %}
{% block extrahead %}{% endblock %}
</head>
{% load i18n %}
<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}">
<!-- Container -->
<div id="container">
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% block breadcrumbs %}<div class="breadcrumbs"><a href="/">{% trans 'Home' %}</a>{% if title %} › {{ title }}{% endif %}</div>{% endblock %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear" />
</div>
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
</div>
<!-- END Container -->
</body>
base_site.html
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
{% block branding %}
<h1 id="site-name">
{% trans 'Customized Admin' %}
</h1>
{% endblock %}
{% block dashboard %}
{% endblock %}
{% block nav-global %}{% endblock %}
已有项目的调研
grappelli 用起来挺不错的。尤其是那个bookmarks很有用。至于navigation,感觉应该添加到每一个app的首页。
django admin自定义相关推荐
- CRM系统-----学员管理系统---admin自定义开发3
admin的自定制开发 18.django实现自定义用户认证 1 from django.db import models 2 from django.contrib.auth.models impo ...
- Django Admin 上传多张图片并显示缩略图
Django Admin 上传多张图片并显示缩略图 文章目录 Django Admin 上传多张图片并显示缩略图 1.效果预览 2.自定义Widget 3.定义模型和模型表单 3.1 定义模型 3.2 ...
- Django Admin 管理工具, 以及问题解决
http://www.runoob.com/django/django-admin-manage-tool.html 登录进去却提示"你无权修改任何东西" urls.py 中加入一 ...
- django admin celery beat简单的定时任务管理平台
一直都在想写一个这样平台,前端比较low,所以就使用了Django 自带的后端写了一个定时任务管理平台 具体结构如下 djangotask/ ├── app01 │?? ├── admin.py │? ...
- Django admin操作
无名小妖 昵称:无名小妖 园龄:1年6个月 粉丝:22 关注:1 +加关注 搜索 常用链接 我的随笔 我的评论 我的参与 最新评论 我的标签 我的标签 Python(1) python3( ...
- Django Admin界面美化第三方插件库测评(多图)
Django自带的admin管理后台简直就是bug级的存在,几行代码就可以撸出一个功能齐全的管理后台来,就像诸葛亮的媳妇黄月英,拥有和诸葛亮一样的才华, 只是不漂亮.对于内部人员使用的管理系统,业务功 ...
- Django admin 管理工具
Django admin 管理工具 1 admin组件的使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 se ...
- django中自定义标签和过滤器
django中自定义标签和过滤器 原文:http://www.cnblogs.com/MnCu8261/p/5934203.html 纪念如何填了半天的坑,红色重点 想要实现自定义标签和过滤器需要进行 ...
- 【Python】解决Django Admin管理界面样式表(CSS Style)丢失问题
[Python]解决Django Admin管理界面样式表(CSS Style)丢失问题 参考文章: (1)[Python]解决Django Admin管理界面样式表(CSS Style)丢失问题 ( ...
最新文章
- Python20-Day02
- 链表问题11——两个单链表相交的系列问题(一):找到有环链表的环入口节点
- mysql错误日志/var/log/mariadb/mariadb.log,二进制日志
- Python-turtle标准库知识小结(python绘图工具)
- 【SpringBoot零基础案例05】【IEDA 2021.1】若SpringBoot项目两种配置文件同时存在,哪种文件配置起作用?
- 冯诺依曼计算机结构教案,冯诺依曼结构的计算机-同济大学精品课程.PPT
- java- WatchService监控
- Linux工作笔记033---Linux(CentOS7)安装zip、unzip命令
- asp.net中用LinkButton取到gridview中当前行的ID值
- 网络设备自动化运维工具——ansible入门笔记
- 一个非计算机专业的 软考初级 程序员考试之路
- Tbase 源码 (六)
- Redmine使用介绍
- 网易云课堂计算机专业,网易云课堂“计算机专业课程”开课
- Windbg线上问题分析:生产环境应用高CPU问题分析
- 命令提示符打不开python_Windows-Python在命令提示符下不起作用?
- ADF用户页面行为监控
- PhotoShop批量生成App大小不同的Icon
- 巧妙复制网页中的文本——复制网页上不能复制的文字
- Pycharm专业版下载、安装、与Anaconda配置、中文化及字体设置、Cracking(自行翻译)方法