当前位置: 首页 > news >正文

Bootstrap your Django admin in 3 minutes

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

What you'll get

  • a responsive interface (because Bootstrap is)

  • an heavy rewrite of the original django-admin templates, but with compatibility in mind.

  • collapsable elements

  • goodies like application name 'translations' without using the {% trans %} tag (to be documented)

  • you can choose between Bootstrap 2 and Bootstrap 3

Requirements

Django >= 1.4.x

Source code

On Github of course: https://github.com/riccardo-forina/django-admin-bootstrapped

Installation

  • pip install django-admin-bootstrapped (virtualenv highly suggested)

  • add django_admin_bootstrapped into the INSTALLED_APPS list beforedjango.contrib.admin

  • have fun!

Your INSTALLED_APPS should look like this:

INSTALLED_APPS = (
    'django_admin_bootstrapped',
    'django.contrib.admin',

    ...
)

Switch to Bootstrap3

Available from version 1.6.2: Do the previous steps, then add'django_admin_bootstrapped.bootstrap3' into the INSTALLED_APPS list before'django_admin_bootstrapped'.

Your INSTALLED_APPS should look like this:

INSTALLED_APPS = (
    'django_admin_bootstrapped.bootstrap3',
    'django_admin_bootstrapped',
    'django.contrib.admin',

    ...
)

23091747_t1Nw.png

Goodies

Translate/change an application name with a template

With the default admin you can't change the application name, but django-admin-bootstrapped let you do it in a really easy way. Just create a file named admin_app_name.html into the application's template folder. Eg: myapp/templates/admin_app_name.html orproject/templates/myapp/admin_app_name.html.

Add custom html to the change form of any model with a template

You can inject custom html on top of any change form creating a template namedadmin_model_MODELNAME_change_form.html into the application's template folder. Eg:myapp/templates/admin_model_mymodelname_change_form.html orproject/templates/myapp/admin_model_mymodelname_change_form.html.

Inline sortable

You can add drag&drop sorting capability to any inline with a couple of changes to your code.

First, add a position field in your model (and sort your model accordingly), for example:

class TestSortable(models.Model):
    that = models.ForeignKey(TestMe)
    position = models.PositiveSmallIntegerField("Position")
    test_char = models.CharField(max_length=5)

    class Meta:
        ordering = ('position', )

Then in your admin.py create a class to handle the inline using thedjango_admin_bootstrapped.admin.models.SortableInline mixin, like this:

from django_admin_bootstrapped.admin.models import SortableInline
from models import TestSortable

class TestSortable(admin.StackedInline, SortableInline):
    model = TestSortable
    extra = 0

You can now use the inline as usual. The result will look like this:

23091748_3vZZ.png

This feature was brought to you by Kyle Bock. Thank you Kyle!

XHTML Compatible

Compatible with both html and xhtml. To enable xhtml for your django app add the following to your settings.py: DEFAULT_CONTENT_TYPE = 'application/xhtml+xml'

Generic lookups in admin

All that needs to be done is change the admin widget with either formfield_overrides like this:

from django_admin_bootstrapped.widgets import GenericContentTypeSelect

class SomeModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.ForeignKey: {'widget': GenericContentTypeSelect},
    }

Or if you want to be more specific:

from django_admin_bootstrapped.widgets import GenericContentTypeSelect

class SomeModelAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content_type':
            kwargs['widget'] = GenericContentTypeSelect
        return super(SomeModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)

If you decide on using formfield_overrides you should be aware of its limitations with relation fields.

This feature (and many more) was brought to you by Jacob Magnusson. Thank you Jacob!

Updates

==== 1.6.2 (2013-11-28) ====

  • Bootstrap 3 theme (thanks to ze-phyr-us). In order to use it, adddjango_admin_bootstrapped.bootstrap3 before django_admin_bootstrapped into yourINSTALLED_APPS setting.

==== 1.6.1 (2013-11-16) ====

  • FIX #56: jquery now loaded from local installation, not the CDN

  • FIX #65: recent actions now has working urls

==== 1.6.0 (2013-11-08) ====

  • django 1.6 compatibility

  • experimental support to Django-CMS 2.4. This is thanks to a fork of kayluhb, which has never been transformed in a PR, but still it's worth a try.

  • Python 3 compatibility (it has always been compatible, but now it's flagged appropriately as such)

  • changed version to 1.6 to reflect the 'Stable' status of the project and the compatibility with Django 1.6

==== 0.4.3 (2013-08-27) ====

  • display errors properly for inlines (thanks to jmagnusson)

==== 0.4.2 (2013-08-10) ====

  • checkboxes now properly aligned

  • admin title translation now working with a dedicated template

==== 0.4.1 (2013-05-29) ====

  • tooltips for title attributes

  • generic relation lookup plugin

  • FIX: Twitter Bootstrap Responsive Navbar Broken on Small Screens (http://stackoverflow.com/questions/10185384/twitter-bootstrap-responsive-navbar-broken-on-small-screens) as reported by Fernando (https://twitter.com/fersan3)

  • FIX: login page responsive layout

  • FIX #6: autocapitalization disabled in the login form (mobile friendly)

  • FIX #13: boolean fields will show the help text if provided

  • FIX #36: list filters now partially usable on a mobile fine

==== 0.4 (2013-05-27) ====

  • added compatibility for django-cms (thanks to kayluhb)

  • xhtml cleanup (thanks to Ricklef Wohlers)

  • UI cleanup (thanks to intuxicated & yceruto)

  • Twitter Bootstrap version 2.3.1 (thanks to yceruto)

  • admin static files now loaded using relative urls (thanks to jmagnusson)

==== 0.3.2 (2013-02-06) ====

  • temporary bugfix for change_list actions

==== 0.3.1 (2013-01-19) ====

  • registration template fixes

  • registration template fixes

==== 0.3 (2013-01-17) ====

  • dropped tabs in homepage in favour of vanilla list of available applications

  • application dropdown menu for fast access in navbar

  • filters moved to a dropdown in navbar to gain screen space

  • added documentation for the translation goodies

==== 0.2 (2012-12-04) ====

  • sortable inlines

  • inlines wrapped in wells for UI consistency

==== 0.1.1 (2012-11-20) ====

  • added a test project

  • upgrade to Twitter Bootstrap 2.2.1 (issue #8)

  • implemented inline tabular view

  • fixed the messages styling

  • temporary fix for the page title

  • open is now the default style for the stacked inlines

  • filters in the change list views are now positioned on the left, to handle wide tables

  • pagination rendered with a smaller style (issue #2)

==== 0.1.0 (2012-10-22) ====

  • first release

Contribute!

This is not a complete project. I know for sure that the tabular inline form wil not work, because I didn't style it. Surely there will be edge cases, but still this is a project I used in production. Unluckly my time is quite limited, so every kind of help will be gladly appreciated!

Screenshots

Homepage

23091752_mTGM.png

List view with filters in dropdown

23091756_ie0z.png

Change form view

23091757_Rj9k.png

Get involved

Feel free to leave your comment, but please remember that these thoughts and opinions are my own, and not that of my employer.


转载于:https://my.oschina.net/zhangrf/blog/225362

相关文章:

  • CALayer动画专题
  • 虽然看的一知半解,但是感觉有一天用到时会很有用,转
  • B1016. 部分A+B (15)
  • OSGi与第一层语义
  • 如何避免TiddlyWiki变慢
  • 山寨一个 Promise
  • 重写、覆盖、重载、多态几个概念的区别分析
  • Ankara prefabrik evler
  • oracle 简单SQL
  • 快速安装配置zabbix_agent端
  • hdu 1754:I Hate It(线段树,入门题,RMQ问题)
  • Unity加载模块深度解析(Shader篇)
  • Android自带SipDemo详解 4
  • nodejs小问题拾遗
  • 李寒峰:微信支付-无法阻挡的生活潮流
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • Android Studio:GIT提交项目到远程仓库
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • CSS盒模型深入
  • iOS小技巧之UIImagePickerController实现头像选择
  • JAVA SE 6 GC调优笔记
  • Java多态
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • Laravel 中的一个后期静态绑定
  • mongo索引构建
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Spring Boot快速入门(一):Hello Spring Boot
  • SpringCloud集成分布式事务LCN (一)
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 测试开发系类之接口自动化测试
  • 聊一聊前端的监控
  • 如何使用 JavaScript 解析 URL
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 适配mpvue平台的的微信小程序日历组件mpvue-calendar
  • 学习HTTP相关知识笔记
  • 延迟脚本的方式
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • ​io --- 处理流的核心工具​
  • ​Spring Boot 分片上传文件
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #、%和$符号在OGNL表达式中经常出现
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #etcd#安装时出错
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (20050108)又读《平凡的世界》
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (C)一些题4
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (免费分享)基于springboot,vue疗养中心管理系统
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置