Working with the django admin

1,722 views

Published on

“Two scoops of django - best practices 1.5” Ch15

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,722
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Working with the django admin

  1. 1. Michelle Leu @flywindy 2014/02/11
  2. 2. 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.
  3. 3. 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
  4. 4. VIEWING STRING REPRESENTATIONS OF OBJECTS •  Implement the __unicode__ () >>> IceCreamBar.objects.all() [<IceCreamBar: Vanilla Crisp>, <IceCreamBar: Mint Cookie Crunch>, <IceCreamBar: Strawberry Pie>]
  5. 5. 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)
  6. 6. VIEWING STRING REPRESENTATIONS OF OBJECTS •  Use list_display
  7. 7. VIEWING STRING REPRESENTATIONS OF OBJECTS •  search_fields •  list_filter •  date_hierarchy •  ordering
  8. 8. 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)
  9. 9. ADDING CALLABLES TO MODELADMIN CLASSES
  10. 10. OTHER FRAMEWORKS   Flask-admin   plays nicely with various ORM's, including SQLAlchemy, MongoEngine, pymongo and Peewee.   file management interface   redis client console
  11. 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. 12. 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
  13. 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. 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. 15. 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/

×