SlideShare 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 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

More Related Content

Similar to Setting a baseline for your django projects

django part-1
django part-1django part-1
django part-1
Gaurav Dixit
 
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 scriptAmit Thakkar
 
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
JoomlaDay Australia
 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdf
jayarao21
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
Shrinath Shenoy
 
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
 
django
djangodjango
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...
Luis Valencia
 
Django interview Questions| Edureka
Django interview  Questions| EdurekaDjango interview  Questions| Edureka
Django interview Questions| Edureka
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)
Vincent Chien
 
Tango with django
Tango with djangoTango with django
Tango with django
Rajan Kumar Upadhyay
 
DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT
DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT
DIPLOMA IN DESIGNING AND WEBSITE DEVELOPMENT
Shri Prakash Pandey
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
Pham Huy Tung
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
Ksd Che
 
Pluggable patterns
Pluggable patternsPluggable patterns
Pluggable patternsCorey Oordt
 
Database Website on Django
Database Website on DjangoDatabase Website on Django
Database Website on Django
HamdaAnees
 
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
Vicki Davis
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Joaquim Rocha
 
Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptx
SHAHZAIBABBAS13
 

Similar to 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
 

Recently uploaded

Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 

Recently uploaded (20)

Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 

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

Editor's Notes

  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