SlideShare a Scribd company logo
1 of 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

More Related Content

Similar to 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 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 experienceJoomlaDay Australia
 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdfjayarao21
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics PresentationShrinath 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 & Djangole980895
 
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| EdurekaEdureka!
 
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
 
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 PatternsPham Huy Tung
 
Django tutorial
Django tutorialDjango tutorial
Django tutorialKsd Che
 
Pluggable patterns
Pluggable patternsPluggable patterns
Pluggable patternsCorey Oordt
 
Database Website on Django
Database Website on DjangoDatabase Website on Django
Database Website on DjangoHamdaAnees
 
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 ClassroomVicki Davis
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJoaquim Rocha
 
Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptxSHAHZAIBABBAS13
 

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

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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