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

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

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 

Dernier (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 

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