Django Admin: Widgetry & Witchery
Upcoming SlideShare
Loading in...5
×
 

Django Admin: Widgetry & Witchery

on

  • 1,718 views

Why we chose to use Django admin, and how it worked, and, well, how it didn't work.

Why we chose to use Django admin, and how it worked, and, well, how it didn't work.

Statistics

Views

Total Views
1,718
Slideshare-icon Views on SlideShare
1,718
Embed Views
0

Actions

Likes
3
Downloads
17
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Django Admin: Widgetry & Witchery Django Admin: Widgetry & Witchery Presentation Transcript

  • Django AdminWidgetry & WitcheryPamela Fox@pamelafox
  • Coursera: What we do
  • Our Backend
  • Why We Need Admin
  • Why Django Admin?Creates forms for adding/editing/searching modelsRestricts fields based on admin roles
  • How Django Admin Works from django.contrib import admin from app import admin from app.courses.models import Course from app.courses.forms import CourseAdminForm class CourseAdmin(ModelAdmin): base_model = Course restrict_fields = [instructors, teaching_assistants, ] form = CourseAdminForm fieldsets = [ (None, { fields: [ name, topic, active, ] }), (Dates, { fields: [ start_date, end_date, start_date_string, duration_string, ] }) ] admin.site.register(Course, CourseAdmin)https://docs.djangoproject.com/en/dev/ref/contrib/admin/
  • ...And a few words onhow it doesn’t work.
  • ☹: The Look & Feel !=
  • Solution: Twitter Bootstraphttps://github.com/gkuhn1/django-admin-templates-twitter-bootstrap
  • ☹: The Default WidgetsBooleanField FilePathField RegexFieldCharField FloatField SlugFieldChoiceField ImageField TimeFieldTypedChoiceField IntegerField URLFieldDateField IPAddressField ComboFieldDateTimeField GenericIPAddressField MultiValueFieldDecimalField MultipleChoiceField SplitDateTimeFieldEmailField TypedMultipleChoiceField ModelChoiceFieldFileField NullBooleanField ModelMultipleChoiceField
  • Solution: Custom WidgetsTransloaditUpload WysiHTMLEditorNumberField NumberRangeField AutoCompleteTextInput UniqueShortName
  • Custom Widgets admin/common/widgets.py:class NumberField(HiddenInput): class Media: js = ( settings.ADMIN_MEDIA_PREFIX + js/numberfields.js, ) def render(self, name, value, attrs=None): input = super(NumberField, self).render(name, value, attrs=attrs) final_attrs = self.build_attrs(attrs) units = final_attrs.get(units, ) html = u""" <div class="number-field"> %(input)s <input type="number" min="1" class="number-range-field-num input-mini"> <span class="number-range-field-units">%(units)s<span> </div> """ % {input: input, units: units} return mark_safe(html) course/admin.py course/forms.pyfrom app import admin from django.forms import ModelFormfrom app.courses.models import Course from app.common.widgets import NumberFieldfrom app.courses.forms import CourseAdminForm class CourseAdminForm(ModelForm):class CourseAdmin(ModelAdmin): class Meta: base_model = Course widgets = { duration_string: NumberField( form = CourseAdminForm attrs={units: weeks}) }
  • ☹: Default Save Options !=
  • Solution: Horrible Hacks templates/admin/change_form.htmlvar topicPageRegEx = //topics/topic//i;var isTopicPage = topicPageRegEx.exec(window.location.href);if (isTopicPage) { var previewHosts = {admin: site, admin.coursera.org: www.coursera.org}; var previewUrl = http:// + previewHosts[window.location.host] + /course/ + $(input[name="short_name"]).val(); var $previewUrl = $(<input type="hidden" name="_previewurl">).val(previewUrl); var $previewButton = $(<input type="submit" name="_saveandpreview" value="Save and Preview" class="btnbtn-info">); var $saveButton = $(.form-actions input[name="_save"]) $saveButton.after(&nbsp;).after($previewButton) .after(&nbsp;).after($previewUrl);} admin/options.pyif "_saveandpreview" in request.POST: return HttpResponseRedirect(request.POST[_previewurl])
  • In conclusion...
  • Our Future Admin Stack?https://github.com/PaulUithol/backbone-tastypie https://github.com/joshbohde/django-backbone-example