SlideShare une entreprise Scribd logo
1  sur  15
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 find 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
We’ll 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 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 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 fields, widgets, mixins,
etc that have evolved over the past 5 years
More coming soon, I’ve kept it light for this talk
Available to install from pypi, source is on bitbucket.org
Iteration 3

Change our model fields to implement our library
Without a single custom form defined in our project, our
form fields have now been flavoured 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
fields and South for migrations, you usually need to do some
extra work for each field
   http://south.readthedocs.org/en/latest/tutorial/
   part4.html#simple-inheritance
Oh no, that’s more boilerplate!
SouthTripleMixin
Add to your custom field’s inheritance structure when you
subclass 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 field
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 SlideShare
   slideshare.net/goodtune/setting-a-baseline-for-your-
   django-projects

Contenu connexe

Similaire à Setting a baseline for your django projects

Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java script
Amit Thakkar
 
Django for Professionals Production websites with Python & Django
Django for Professionals Production websites with Python & DjangoDjango for Professionals Production websites with Python & Django
Django for Professionals Production websites with Python & Django
le980895
 
Pluggable patterns
Pluggable patternsPluggable patterns
Pluggable patterns
Corey Oordt
 

Similaire à Setting a baseline for your django projects (20)

django part-1
django part-1django part-1
django part-1
 
Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java script
 
Top tips from what we've learned from our 10 years experience
Top tips from what we've learned from our 10 years experienceTop tips from what we've learned from our 10 years experience
Top tips from what we've learned from our 10 years experience
 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdf
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Django for Professionals Production websites with Python & Django
Django for Professionals Production websites with Python & DjangoDjango for Professionals Production websites with Python & Django
Django for Professionals Production websites with Python & Django
 
Django introduction
Django introductionDjango introduction
Django introduction
 
django
djangodjango
django
 
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
 
Django interview Questions| Edureka
Django interview  Questions| EdurekaDjango interview  Questions| Edureka
Django interview Questions| Edureka
 
The Django Book chapter 4 templates (supplement)
The Django Book chapter 4 templates (supplement)The Django Book chapter 4 templates (supplement)
The Django Book chapter 4 templates (supplement)
 
Tango with django
Tango with djangoTango with django
Tango with django
 
DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT
DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT
DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
 
Pluggable patterns
Pluggable patternsPluggable patterns
Pluggable patterns
 
Database Website on Django
Database Website on DjangoDatabase Website on Django
Database Website on Django
 
2014 reinventing-writing at #reinvent14 Reinventing the Classroom
2014 reinventing-writing at #reinvent14 Reinventing the Classroom2014 reinventing-writing at #reinvent14 Reinventing the Classroom
2014 reinventing-writing at #reinvent14 Reinventing the Classroom
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptx
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

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 find 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 We’ll 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 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 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 fields, widgets, mixins, etc that have evolved over the past 5 years More coming soon, I’ve kept it light for this talk Available to install from pypi, source is on bitbucket.org
  • 9. Iteration 3 Change our model fields to implement our library Without a single custom form defined in our project, our form fields have now been flavoured 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 fields and South for migrations, you usually need to do some extra work for each field http://south.readthedocs.org/en/latest/tutorial/ part4.html#simple-inheritance Oh no, that’s more boilerplate!
  • 13. SouthTripleMixin Add to your custom field’s inheritance structure when you subclass 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 field 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 SlideShare slideshare.net/goodtune/setting-a-baseline-for-your- django-projects

Notes de l'éditeur

  1. \n
  2. \n
  3. 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
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n