15
Michelle Leu @flywindy 2014/02/11

Working with the django admin

Embed Size (px)

DESCRIPTION

“Two scoops of django - best practices 1.5” Ch15

Citation preview

Page 1: Working with the django admin

Michelle Leu @flywindy 2014/02/11

Page 2: Working with the django admin

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.

Page 3: Working with 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

Page 4: Working with the django admin

VIEWING STRING REPRESENTATIONS OF OBJECTS •  Implement the __unicode__ ()

>>> IceCreamBar.objects.all() [<IceCreamBar: Vanilla Crisp>, <IceCreamBar: Mint Cookie Crunch>, <IceCreamBar: Strawberry Pie>]

Page 5: Working with the django admin

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)

Page 6: Working with the django admin

VIEWING STRING REPRESENTATIONS OF OBJECTS •  Use list_display

Page 7: Working with the django admin

VIEWING STRING REPRESENTATIONS OF OBJECTS •  search_fields •  list_filter •  date_hierarchy •  ordering

Page 8: Working with the django admin

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)

Page 9: Working with the django admin

ADDING CALLABLES TO MODELADMIN CLASSES

Page 10: Working with the django admin

OTHER FRAMEWORKS

  Flask-admin   plays nicely with various ORM's, including SQLAlchemy, MongoEngine,

pymongo and Peewee.   file management interface   redis client console

Page 11: Working with the django admin

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.

Page 12: Working with the django admin

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

Page 13: Working with the django admin

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

Page 14: Working with the django admin

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

Page 15: Working with the django admin

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/