Upgrade to Pro — share decks privately, control downloads, hide ads and more …

django unleashed

django unleashed

Created them for a hands on django session.

Saket Bhushan

April 20, 2013
Tweet

More Decks by Saket Bhushan

Other Decks in Programming

Transcript

  1. Topics Covered • Setting up your environment • Project Scaffolding

    • Overview of django • Dive into Settings File • Take some Notes • Testing your application • Some best practices • Example Repo [https://github. com/fRuiApps/djbp]
  2. Topics not Covered • Documenting your Code • Deploying your

    Code • Performance Benchmarking & Optimization • Django Forms
  3. Your Environment • Isolation • Determinism • Similarity • pip

    • virtualenv • virtualenvwrapper Tutorial
  4. Your Environment • sudo apt-get install python-setuptools python-dev build-essential •

    easy_install -U pip • pip install virtualenv virtualenvwrapper • requirements file ◦ local ◦ test ◦ production
  5. Installation & Getting Started • pip install -r requirements/test.txt •

    pip install -r requirements/local.txt • django-admin.py startproject djnotes • python manage.py startapp noteapp -- settings=djnotes.settings.local
  6. Project Structure ├── djnotes │ └── settings ├── noteapp │

    ├── migrations │ ├── templates │ └── tests ├── static │ ├── css │ ├── img │ └── js │ ├── lib │ └── modules └── templates
  7. Settings File • Settings under version control • Use Relative

    Path ◦ no hardcoded paths eg. /home/betaversion/... • Keep secret keys Secret • Different Settings for different environment • Custom Middlewares & ContextProcessors in a try catch block • local_settings.py is an AntiPattern
  8. Some more of Django • Views - Presentation Logic •

    Models - Business Logic • Templates - Presentation Layer
  9. Intro to TDD • Unit vs Integration Test • Test

    Runner • What all to test? Detailed Article(s) on TDD Slides from my PyCon talk on TDD
  10. Some minimal best practices • Models ◦ The database fields

    should be names as first_name and NOT FirstName. ◦ The database table names(class names) should be named as ClassNames and not class_names ◦ There should be a unicode method for each of the classes.
  11. Some minimal best practices • Templates ◦ One base.html at

    the project level, and one base. html at each of the app levels. ◦ App level base.html should extend the project level base.html ◦ The variable in templates should have spaces between the name and bracket. Eg. {{ variable }} and NOT {{variable}}
  12. Some minimal best practices • urls ◦ design pretty urls

    ▪ use slugs instead of primary keys ◦ Each of the urls should have a name, corresponding to the view name. ◦ There should be one urls.py at the project level, and one urls.py at each app level. The project level urls. py should include each of the urls.py under a prefix. Further Reading