Michelle Leu
@flywindy
2014/02/11
WHO AND WHEN TO USE THE DJANGO ADMIN
When people ask, “What are the benefits of Django over other web frameworks?”
the admin is what usually comes to mind.
Who
•  It's Not for End Users
When
•  It’s usually not worth it to heavily customize the Django admin.
VIEWING STRING REPRESENTATIONS OF
OBJECTS
•  Implement the __unicode__ ()
#EXAMPLE 15.1
from django.db import models
class IceCreamBar(models.Model):
name = models.CharField(max_length=100)
shell = models.CharField(max_length=100)
filling = models.CharField(max_length=100)
has_stick = models.BooleanField(default=True)
def __unicode__(self):
return self.name
VIEWING STRING REPRESENTATIONS OF
OBJECTS
•  Implement the __unicode__ ()
>>> IceCreamBar.objects.all()
[<IceCreamBar: Vanilla Crisp>, <IceCreamBar: Mint Cookie Crunch>,
<IceCreamBar: Strawberry Pie>]
VIEWING STRING REPRESENTATIONS OF
OBJECTS
•  Use list_display
#EXAMPLE 15.3
from django.contrib import admin
from .models import IceCreamBar
class IceCreamBarAdmin(admin.ModelAdmin):
list_display = ("name", "shell", "filling”,)
admin.site.register(IceCreamBar, IceCreamBarAdmin)
VIEWING STRING REPRESENTATIONS OF
OBJECTS
•  Use list_display
VIEWING STRING REPRESENTATIONS OF
OBJECTS
•  search_fields
•  list_filter
•  date_hierarchy
•  ordering
ADDING CALLABLES TO MODELADMIN CLASSES
#EXAMPLE 15.4
from django.contrib import admin
from django.core.urlresolvers import reverse
from icecreambars.models import IceCreamBar
class IceCreamBarAdmin(admin.ModelAdmin):
list_display = ("name", "shell", "filling",)
readonly_fields = ("show_url",)
def show_url(self, instance):
url = reverse("ice_cream_bar_detail", kwargs={"pk": instance.pk})
response = """<a href="{0}">{0}</a>""".format(url)
return response
show_url.short_description = "Ice Cream Bar URL"
# Displays HTML tags
# Never set allow_tags to True against user submitted data!!!
show_url.allow_tags = True
admin.site.register(IceCreamBar, IceCreamBarAdmin)
ADDING CALLABLES TO MODELADMIN CLASSES
OTHER FRAMEWORKS
  Flask-admin
  plays nicely with various ORM's, including SQLAlchemy, MongoEngine,
pymongo and Peewee.
  file management interface
  redis client console
OTHER FRAMEWORKS
  Web2py
  Web-Based IDE: Create, modify, deploy and manage application from
anywhere using your browser. One web2py instance can run multiple web sites
using different databases.
DJANGO ADMIN PLUGIN
django-xadmin(by sshwsfc)
•  Drop-in replacement of Django admin
•  Twitter Bootstrap based UI with theme support
•  Extensible with plugin support
•  Better filter, date range, number range, etc.
•  Built-in data export with xls, csv, xml and json format
•  Dashboard page with widget support
•  In-site bookmarking
•  Full CRUD methods
•  Documentation: Chinese
http://demo.xadmin.io
DJANGO ADMIN PLUGIN
django-grappelli (by sehmaschine)
•  Clean and consistent look & feel
•  Related lookups with Autocompletes
•  Inline sortables with Drag & Drop
•  Compass-based CSS
• Integrated TinyMCE theme
• Customizable dashboard
• Improved collapsibles
• Unified jQuery-Plugins
• Compatible with django—filebrowser
DJANGO ADMIN PLUGIN
django-admin2 (by pydanny)
•  Rewrite of the Django Admin backend
•  Drop-in themes
•  Built-in RESTful API
•  (django-rest-framework)
django-admin-bootstrapped
•  Bootstrap theme
REFERENCE
  Django official doc: https://docs.djangoproject.com/en/dev/ref/contrib/admin/
  Django Book: http://django-book.readthedocs.org/en/latest/chapter06.html
  Flask-admin doc: https://flask-admin.readthedocs.org/en/latest/
  Flask-admin example: http://examples.flask-admin.org/
  django-xadmin: https://github.com/sshwsfc/django-xadmin
  django-grappelli: http://grappelliproject.com/
  Django Packages - Admin Interface:
https://www.djangopackages.com/grids/g/admin-interface/

Working with the django admin

  • 1.
  • 2.
    WHO AND WHENTO USE THE DJANGO ADMIN When people ask, “What are the benefits of Django over other web frameworks?” the admin is what usually comes to mind. Who •  It's Not for End Users When •  It’s usually not worth it to heavily customize the Django admin.
  • 3.
    VIEWING STRING REPRESENTATIONSOF OBJECTS •  Implement the __unicode__ () #EXAMPLE 15.1 from django.db import models class IceCreamBar(models.Model): name = models.CharField(max_length=100) shell = models.CharField(max_length=100) filling = models.CharField(max_length=100) has_stick = models.BooleanField(default=True) def __unicode__(self): return self.name
  • 4.
    VIEWING STRING REPRESENTATIONSOF OBJECTS •  Implement the __unicode__ () >>> IceCreamBar.objects.all() [<IceCreamBar: Vanilla Crisp>, <IceCreamBar: Mint Cookie Crunch>, <IceCreamBar: Strawberry Pie>]
  • 5.
    VIEWING STRING REPRESENTATIONSOF OBJECTS •  Use list_display #EXAMPLE 15.3 from django.contrib import admin from .models import IceCreamBar class IceCreamBarAdmin(admin.ModelAdmin): list_display = ("name", "shell", "filling”,) admin.site.register(IceCreamBar, IceCreamBarAdmin)
  • 6.
    VIEWING STRING REPRESENTATIONSOF OBJECTS •  Use list_display
  • 7.
    VIEWING STRING REPRESENTATIONSOF OBJECTS •  search_fields •  list_filter •  date_hierarchy •  ordering
  • 8.
    ADDING CALLABLES TOMODELADMIN CLASSES #EXAMPLE 15.4 from django.contrib import admin from django.core.urlresolvers import reverse from icecreambars.models import IceCreamBar class IceCreamBarAdmin(admin.ModelAdmin): list_display = ("name", "shell", "filling",) readonly_fields = ("show_url",) def show_url(self, instance): url = reverse("ice_cream_bar_detail", kwargs={"pk": instance.pk}) response = """<a href="{0}">{0}</a>""".format(url) return response show_url.short_description = "Ice Cream Bar URL" # Displays HTML tags # Never set allow_tags to True against user submitted data!!! show_url.allow_tags = True admin.site.register(IceCreamBar, IceCreamBarAdmin)
  • 9.
    ADDING CALLABLES TOMODELADMIN CLASSES
  • 10.
    OTHER FRAMEWORKS   Flask-admin  plays nicely with various ORM's, including SQLAlchemy, MongoEngine, pymongo and Peewee.   file management interface   redis client console
  • 11.
    OTHER FRAMEWORKS   Web2py  Web-Based IDE: Create, modify, deploy and manage application from anywhere using your browser. One web2py instance can run multiple web sites using different databases.
  • 12.
    DJANGO ADMIN PLUGIN django-xadmin(bysshwsfc) •  Drop-in replacement of Django admin •  Twitter Bootstrap based UI with theme support •  Extensible with plugin support •  Better filter, date range, number range, etc. •  Built-in data export with xls, csv, xml and json format •  Dashboard page with widget support •  In-site bookmarking •  Full CRUD methods •  Documentation: Chinese http://demo.xadmin.io
  • 13.
    DJANGO ADMIN PLUGIN django-grappelli(by sehmaschine) •  Clean and consistent look & feel •  Related lookups with Autocompletes •  Inline sortables with Drag & Drop •  Compass-based CSS • Integrated TinyMCE theme • Customizable dashboard • Improved collapsibles • Unified jQuery-Plugins • Compatible with django—filebrowser
  • 14.
    DJANGO ADMIN PLUGIN django-admin2(by pydanny) •  Rewrite of the Django Admin backend •  Drop-in themes •  Built-in RESTful API •  (django-rest-framework) django-admin-bootstrapped •  Bootstrap theme
  • 15.
    REFERENCE   Django officialdoc: https://docs.djangoproject.com/en/dev/ref/contrib/admin/   Django Book: http://django-book.readthedocs.org/en/latest/chapter06.html   Flask-admin doc: https://flask-admin.readthedocs.org/en/latest/   Flask-admin example: http://examples.flask-admin.org/   django-xadmin: https://github.com/sshwsfc/django-xadmin   django-grappelli: http://grappelliproject.com/   Django Packages - Admin Interface: https://www.djangopackages.com/grids/g/admin-interface/