Setting a baseline foryour Django projects        Gary Reynolds       Touch Technology            @goodtune      bitbucket...
DRY principleDjango has loads of ways to prevent repetitive boilerplate   ModelForm   Class Based Views   django.contrib.a...
from django import formsfrom .models import BookBOOLEAN_CHOICES = ((1, Yes), (0, No))class BookForm(forms.ModelForm):    f...
Iteration 0Our example project is for a book storeBooks will need to have   a title   a published date   are either for sa...
Iteration 1Add our Book modelAdd a front end view to output a list of booksRegister the model with django.contrib.adminUse...
Iteration 2Add the published date to the Book modelAdd a view to edit our Book items in the front end
Better Form FieldsDefault form fields for some model fields are a poor choice   BooleanField as a checkbox   DateField, Time...
touchtechnology-publicA backport of useful model & form fields, widgets, mixins,etc that have evolved over the past 5 years...
Iteration 3Change our model fields to implement our libraryWithout a single custom form defined in our project, ourform field...
AuthenticationDo we really want just anyone editing our books?  from    django.contrib.auth.decorators import login_requir...
Iteration 4Change our views to implement our libraryWithout any other changes to our project, our views havenow been prote...
MigrationsWhen you use simple inheritance of built-in Django modelfields and South for migrations, you usually need to do s...
SouthTripleMixinAdd to your custom field’s inheritance structure when yousubclass a built-in Django field   We’re just alter...
Questions?
Sample project and touchtechnology-public can be obtainedfrom bitbucket.org   bitbucket.org/touchtechnology/public   bitbu...
Upcoming SlideShare
Loading in …5
×

Setting a baseline for your django projects

815 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
815
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • This might look trivial, but what if I wanted all my BooleanField to render as radio buttons (which I do).\nI would need to write this boilerplate for every ModelForm that has a BooleanField.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Setting a baseline for your django projects

    1. 1. Setting a baseline foryour Django projects Gary Reynolds Touch Technology @goodtune bitbucket.org/goodtune github.com/goodtune
    2. 2. DRY principleDjango has loads of ways to prevent repetitive boilerplate ModelForm Class Based Views django.contrib.adminThen do I find myself writing code like this?
    3. 3. from django import formsfrom .models import BookBOOLEAN_CHOICES = ((1, Yes), (0, No))class BookForm(forms.ModelForm): for_sale = forms.ChoiceField( choices=BOOLEAN_CHOICES) class Meta: model = Book
    4. 4. Iteration 0Our example project is for a book storeBooks will need to have a title a published date are either for sale or notWe’ll start with an empty project
    5. 5. Iteration 1Add our Book modelAdd a front end view to output a list of booksRegister the model with django.contrib.adminUse south for schema migration
    6. 6. Iteration 2Add the published date to the Book modelAdd a view to edit our Book items in the front end
    7. 7. Better Form FieldsDefault form fields for some model fields are a poor choice BooleanField as a checkbox DateField, TimeField, DateTimeField are all text fields Depending on your project, there will be othersWe can easily override these, build a library, and reuseacross all our applications to suit our preferences
    8. 8. touchtechnology-publicA backport of useful model & form fields, widgets, mixins,etc that have evolved over the past 5 yearsMore coming soon, I’ve kept it light for this talkAvailable to install from pypi, source is on bitbucket.org
    9. 9. Iteration 3Change our model fields to implement our libraryWithout a single custom form defined in our project, ourform fields have now been flavoured to our tasteAdd a view to create from the front end as well
    10. 10. AuthenticationDo we really want just anyone editing our books? from django.contrib.auth.decorators import login_required from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 from django.template.response import TemplateResponse @login_required def book_edit(request, pk): book = get_object_or_404(Book, pk=pk) if request.method == POST: form = BookForm(data=request.POST, instance=book) if form.is_valid(): form.save() return HttpResponseRedirect(..) else: form = BookForm(instance=book) context = {form: form, book: book} return TemplateResponse( request, example/book_form.html, context)
    11. 11. Iteration 4Change our views to implement our libraryWithout any other changes to our project, our views havenow been protected from unauthenticated users
    12. 12. MigrationsWhen you use simple inheritance of built-in Django modelfields and South for migrations, you usually need to do someextra work for each field http://south.readthedocs.org/en/latest/tutorial/ part4.html#simple-inheritanceOh no, that’s more boilerplate!
    13. 13. SouthTripleMixinAdd to your custom field’s inheritance structure when yousubclass a built-in Django field We’re just altering the way we markup our field in forms, not it’s database internal representation Migrations will represent your field as the built-in fieldThis code is stand alone, you can copy it into any project
    14. 14. Questions?
    15. 15. Sample project and touchtechnology-public can be obtainedfrom bitbucket.org bitbucket.org/touchtechnology/public bitbucket.org/goodtune/sydjango-exampleThis presentation can be downloaded from SlideShare slideshare.net/goodtune/setting-a-baseline-for-your- django-projects

    ×