際際滷

際際滷Share a Scribd company logo
Setting a baseline for
your Django projects
        Gary Reynolds
       Touch Technology
            @goodtune
      bitbucket.org/goodtune
       github.com/goodtune
DRY principle

Django has loads of ways to prevent repetitive boilerplate
   ModelForm
   Class Based Views
   django.contrib.admin
Then do I 鍖nd myself writing code like this?
from django import forms
from .models import Book

BOOLEAN_CHOICES = ((1, 'Yes'), (0, 'No'))


class BookForm(forms.ModelForm):

    for_sale = forms.ChoiceField(
        choices=BOOLEAN_CHOICES)

    class Meta:
        model = Book
Iteration 0
Our example project is for a book store
Books will need to have
   a title
   a published date
   are either for sale or not
Well start with an empty project
Iteration 1

Add our Book model
Add a front end view to output a list of books
Register the model with django.contrib.admin
Use south for schema migration
Iteration 2


Add the published date to the Book model
Add a view to edit our Book items in the front end
Better Form Fields
Default form 鍖elds for some model 鍖elds are a poor choice
   BooleanField as a checkbox
   DateField, TimeField, DateTimeField are all text 鍖elds
   Depending on your project, there will be others
We can easily override these, build a library, and reuse
across all our applications to suit our preferences
touchtechnology-public

A backport of useful model & form 鍖elds, widgets, mixins,
etc that have evolved over the past 5 years
More coming soon, Ive kept it light for this talk
Available to install from pypi, source is on bitbucket.org
Iteration 3

Change our model 鍖elds to implement our library
Without a single custom form de鍖ned in our project, our
form 鍖elds have now been 鍖avoured to our taste
Add a view to create from the front end as well
Authentication
Do 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)
Iteration 4


Change our views to implement our library
Without any other changes to our project, our views have
now been protected from unauthenticated users
Migrations

When you use simple inheritance of built-in Django model
鍖elds and South for migrations, you usually need to do some
extra work for each 鍖eld
   http://south.readthedocs.org/en/latest/tutorial/
   part4.html#simple-inheritance
Oh no, thats more boilerplate!
SouthTripleMixin
Add to your custom 鍖elds inheritance structure when you
subclass a built-in Django 鍖eld
   Were just altering the way we markup our 鍖eld in forms,
   not its database internal representation
   Migrations will represent your 鍖eld as the built-in 鍖eld
This code is stand alone, you can copy it into any project
Questions?
Sample project and touchtechnology-public can be obtained
from bitbucket.org
   bitbucket.org/touchtechnology/public
   bitbucket.org/goodtune/sydjango-example
This presentation can be downloaded from 際際滷Share
   slideshare.net/goodtune/setting-a-baseline-for-your-
   django-projects

More Related Content

Setting a baseline for your django projects

  • 1. Setting a baseline for your Django projects Gary Reynolds Touch Technology @goodtune bitbucket.org/goodtune github.com/goodtune
  • 2. DRY principle Django has loads of ways to prevent repetitive boilerplate ModelForm Class Based Views django.contrib.admin Then do I 鍖nd myself writing code like this?
  • 3. from django import forms from .models import Book BOOLEAN_CHOICES = ((1, 'Yes'), (0, 'No')) class BookForm(forms.ModelForm): for_sale = forms.ChoiceField( choices=BOOLEAN_CHOICES) class Meta: model = Book
  • 4. Iteration 0 Our example project is for a book store Books will need to have a title a published date are either for sale or not Well start with an empty project
  • 5. Iteration 1 Add our Book model Add a front end view to output a list of books Register the model with django.contrib.admin Use south for schema migration
  • 6. Iteration 2 Add the published date to the Book model Add a view to edit our Book items in the front end
  • 7. Better Form Fields Default form 鍖elds for some model 鍖elds are a poor choice BooleanField as a checkbox DateField, TimeField, DateTimeField are all text 鍖elds Depending on your project, there will be others We can easily override these, build a library, and reuse across all our applications to suit our preferences
  • 8. touchtechnology-public A backport of useful model & form 鍖elds, widgets, mixins, etc that have evolved over the past 5 years More coming soon, Ive kept it light for this talk Available to install from pypi, source is on bitbucket.org
  • 9. Iteration 3 Change our model 鍖elds to implement our library Without a single custom form de鍖ned in our project, our form 鍖elds have now been 鍖avoured to our taste Add a view to create from the front end as well
  • 10. Authentication Do 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. Iteration 4 Change our views to implement our library Without any other changes to our project, our views have now been protected from unauthenticated users
  • 12. Migrations When you use simple inheritance of built-in Django model 鍖elds and South for migrations, you usually need to do some extra work for each 鍖eld http://south.readthedocs.org/en/latest/tutorial/ part4.html#simple-inheritance Oh no, thats more boilerplate!
  • 13. SouthTripleMixin Add to your custom 鍖elds inheritance structure when you subclass a built-in Django 鍖eld Were just altering the way we markup our 鍖eld in forms, not its database internal representation Migrations will represent your 鍖eld as the built-in 鍖eld This code is stand alone, you can copy it into any project
  • 15. Sample project and touchtechnology-public can be obtained from bitbucket.org bitbucket.org/touchtechnology/public bitbucket.org/goodtune/sydjango-example This presentation can be downloaded from 際際滷Share slideshare.net/goodtune/setting-a-baseline-for-your- django-projects

Editor's Notes

  • #2: \n
  • #3: \n
  • #4: 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
  • #5: \n
  • #6: \n
  • #7: \n
  • #8: \n
  • #9: \n
  • #10: \n
  • #11: \n
  • #12: \n
  • #13: \n
  • #14: \n
  • #15: \n
  • #16: \n