Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Django Admin: Widgetry & Witchery
Search
Pamela Fox
August 31, 2012
Technology
4
1.4k
Django Admin: Widgetry & Witchery
Why we chose to use Django admin, and how it worked, and, well, how it didn't work.
Pamela Fox
August 31, 2012
Tweet
Share
More Decks by Pamela Fox
See All by Pamela Fox
Building a RAG-poweredAI chat appwith Python and VS Code
pamelafox
0
140
Building a RAG app to chat with your data (on Azure)
pamelafox
3
440
Automated evaluations of LLM apps with azure-ai-generative SDK
pamelafox
0
86
AI Chat App Hack: Continuous Deployment of your Chat App
pamelafox
0
95
AI Chat App Hack: Evaluating a Chat App
pamelafox
0
140
Azure AI Search Best Practices for RAG Chat Apps
pamelafox
0
290
Customizing your RAG Chat App
pamelafox
0
350
AI Chat App Hack: Building a RAG Chat App
pamelafox
0
570
Vector search and retrieval for Generative AI app (Microsoft AI Tour SF)
pamelafox
0
930
Other Decks in Technology
See All in Technology
FrontDoorとWebAppsを組み合わせた際のリダイレクト処理の注意点
kenichirokimura
1
720
R3のコードから見る実践LINQ実装最適化・コンカレントプログラミング実例
neuecc
3
2.2k
コードや知識を組み込む / Incorporate Code and knowledge
ks91
PRO
0
140
生成AIの変革の時代に、直近1年で直面した課題とその解決策
ktc_wada
0
510
require(ESM)とECMAScript仕様
uhyo
4
950
実例で紹介するRAG導入時の知見と精度向上の勘所
yamahiro
5
1.5k
Kernel MemoryでAzure OpenAI Serviceとお手軽データソース連携
mitsuzono
1
280
家族アルバム みてねにおけるGrafana活用術 / Grafana Meetup Japan Vol.1 LT
isaoshimizu
1
910
Gradle Build Scanを使ってビルドのことを知ろう potatotips #87
tomorrowkey
2
150
20分で完全に理解するGrafanaダッシュボード
hamadakoji
5
890
Além do else! Categorizando Pokemóns com Pattern Matching no JavaScript
wmsbill
0
700
DMM.com アルファ室採用案内資料
hsugita
1
220
Featured
See All Featured
Bash Introduction
62gerente
605
210k
The Brand Is Dead. Long Live the Brand.
mthomps
49
29k
Making the Leap to Tech Lead
cromwellryan
125
8.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
20
1.7k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
The Invisible Customer
myddelton
114
12k
What's new in Ruby 2.0
geeforr
337
31k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
188
16k
Practical Orchestrator
shlominoach
183
9.7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
Ruby is Unlike a Banana
tanoku
96
10k
Transcript
Django Admin Widgetry & Witchery Pamela Fox @pamelafox Thursday, August
30, 12
Coursera: What we do Thursday, August 30, 12
Our Backend Thursday, August 30, 12
Why We Need Admin Thursday, August 30, 12
Why Django Admin? Creates forms for adding/editing/searching models Restricts fields
based on admin roles Thursday, August 30, 12
How Django Admin Works https://docs.djangoproject.com/en/dev/ref/contrib/admin/ 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) Thursday, August 30, 12
...And a few words on how it doesn’t work. Thursday,
August 30, 12
☹: The Look & Feel != Thursday, August 30, 12
Solution: Twitter Bootstrap https://github.com/gkuhn1/django-admin-templates-twitter-bootstrap Thursday, August 30, 12
☹: The Default Widgets BooleanField CharField ChoiceField TypedChoiceField DateField DateTimeField
DecimalField EmailField FileField FilePathField FloatField ImageField IntegerField IPAddressField GenericIPAddressField MultipleChoiceField TypedMultipleChoiceField NullBooleanField RegexField SlugField TimeField URLField ComboField MultiValueField SplitDateTimeField ModelChoiceField ModelMultipleChoiceField Thursday, August 30, 12
Solution: Custom Widgets WysiHTMLEditor TransloaditUpload UniqueShortName NumberField NumberRangeField AutoCompleteTextInput Thursday,
August 30, 12
Custom Widgets 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) admin/common/widgets.py: from django.forms import ModelForm from app.common.widgets import NumberField class CourseAdminForm(ModelForm): class Meta: widgets = { 'duration_string': NumberField( attrs={'units': 'weeks'}) } course/forms.py from app import admin from app.courses.models import Course from app.courses.forms import CourseAdminForm class CourseAdmin(ModelAdmin): base_model = Course form = CourseAdminForm course/admin.py Thursday, August 30, 12
☹: Default Save Options != Thursday, August 30, 12
Solution: Horrible Hacks var 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="btn btn-info">'); var $saveButton = $('.form-actions input[name="_save"]') $saveButton.after(' ').after($previewButton) .after(' ').after($previewUrl); } templates/admin/change_form.html if "_saveandpreview" in request.POST: return HttpResponseRedirect(request.POST['_previewurl']) admin/options.py Thursday, August 30, 12
In conclusion... Thursday, August 30, 12
Our Future Admin Stack? https://github.com/PaulUithol/backbone-tastypie https://github.com/joshbohde/django-backbone-example Thursday, August 30, 12