Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Django Best Practices
Disclaimer● This talk should take approximately 1 month● I dont always take my own advice● Im not an expert● Im going to f...
A note on personal philosophy● Organization makes chaos manageable○ PEP8○ Code standards○ DRY○ Hierarchical organization l...
A note on personal philosophy● Provide value with as few LOC as possible○ "Everything should be made as simple as possible...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
README● Your project should have a README○ Seriously○ You want to go on vacation● Format it in some way thats easy to read...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Requirements● We use pip, with pretty results● Pin your versions
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Continuous Integration● Run your tests regularly● We use circleci○ Pretty simple○ Integrates with github○ still working ou...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Fabric● Useful for deployment to remote machines○ Supports parallel remote execution● Nice python replacement for○ Bash sc...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Things that shouldnt be in theREPO● Generated Files● Data files● .pyc files● Backup files● Cat pictures○ Ok, maybe cat pic...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
virtualenv● Oh my god use it● Keeps your projects from stepping on eachother● World Peace● Kittens
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Documentation● IMHO lots of docs in the root make a projecthard to navigate● Plaintext format that can generate html docsp...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Project Configuration● Non-sensitive configuration files● Avoid putting these in root, againhierarchical structure
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Newrelic● Makes my life so much better● Monitoring○ Exceptions○ Response Time / Profiling○ Database profiling● Alternative...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Root project urls● If at all possible should only include apps urlpatternsfrom django.conf.urls import patterns, url, incl...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
WSGI● How your webserver actually hooks into yourapp● WSGI middlewares can go here○ Havent ever found a user for them
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Settings● Recently weve started using environmentvariables with some success● Weve also used approaches where we havea set...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Unpackaged dependencies● Whenever possible use pip / setuptools● Put django snippets / projects that arent inpypi in lib/●...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Project specific Code● As much as possible bundle code intoreusable apps● Things that might have to go here○ Custom test r...
Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.p...
Django Applications● Re-usuable apps make Django awesome○ Use relative imports○ Try to not include anything project specif...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Application Tests● You should have tests○ TDD/BDD if possible○ IMHO your model tests / design give better ROI○ If you find...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Models● Models are the lifeblood of your apps○ Start with model design first○ Design your data model well and views will c...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Application Urls● All views accessible in an app should bebundled into an app level urls file● Use named url patterns to s...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Django Admin● Major win● Use django admin for as much as you can● Most aspects are subclassable/extentable● Your business ...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Application Views● Make the views as simple as possible○ Template views can probably be functions that callrender○ If a vi...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Tasty Pie● Dont re-invent the wheel● Tastypie is useful for most (but not all)RESTful applications● The code is already te...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Background / Async in Django● There are lots of choices for this● ZMQ○ Lots of options for routing○ VERY flexible○ VERY fa...
Celery● Should be your default choice for async● Very simple python API○ Async tasks○ Tasks in the future○ Cron replacemen...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pyforms.pymigrations/management/__init...
Django Forms● Model Forms○ DRY○ Handles validation in a declarative way○ Will make your life better● Django crispy forms○ ...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
South● Reproducible migrations○ Schema migrations○ Data migrations○ Test runner○ Probably going to be included in Django 1...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Management Commands● We used to use these for cron jobs○ Celery beat is nicer now● Helpful for project wide scripts that r...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Static Files● Namespace your static files○ app_name/static/app_name○ avoids collisions○ common static files can go in the ...
App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycom...
Application templates● Same as static files, namespace them● Use template inheritance○ DRY● Use named url patterns in your...
Other things you should be aware of● Middlewares● Class Based Views● Template Context Processors● Custom Template Tags
Django Apps that make me happy● Django Extensions○ Werkzeug● South○ Probably coming to Django 1.6
Tools you should use● Code Quality○ PEP8○ Pyflakes○ Pychecker○ jshint○ jslint● Development Environment○ ack-grep○ emacs :)...
What did I forget?
Nächste SlideShare
Wird geladen in …5
×

Django best practices

3.177 Aufrufe

Veröffentlicht am

  • Dating for everyone is here: ♥♥♥ http://bit.ly/2u6xbL5 ♥♥♥
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • Sex in your area is here: ♥♥♥ http://bit.ly/2u6xbL5 ♥♥♥
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Django best practices

  1. 1. Django Best Practices
  2. 2. Disclaimer● This talk should take approximately 1 month● I dont always take my own advice● Im not an expert● Im going to forget something● You have a different opinion than me
  3. 3. A note on personal philosophy● Organization makes chaos manageable○ PEP8○ Code standards○ DRY○ Hierarchical organization lets me understand what Ineed to to solve the problem in front of me
  4. 4. A note on personal philosophy● Provide value with as few LOC as possible○ "Everything should be made as simple as possible,but not simpler"● "Unix Philosophy"○ Read "The Art of Unix Programming"○ Things should fail noisily○ Dont surprise me
  5. 5. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  6. 6. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  7. 7. README● Your project should have a README○ Seriously○ You want to go on vacation● Format it in some way thats easy to read○ duh● What should probably be there○ Installation instructions○ Design decisions○ Contribution information (or put this in a Contributing file)
  8. 8. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  9. 9. Requirements● We use pip, with pretty results● Pin your versions
  10. 10. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  11. 11. Continuous Integration● Run your tests regularly● We use circleci○ Pretty simple○ Integrates with github○ still working out their own bugs● Other options○ Buildbot○ Jenkins○ TravisCI○ Codeship.io
  12. 12. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  13. 13. Fabric● Useful for deployment to remote machines○ Supports parallel remote execution● Nice python replacement for○ Bash scripts○ Makefiles○ Duct Tape
  14. 14. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  15. 15. Things that shouldnt be in theREPO● Generated Files● Data files● .pyc files● Backup files● Cat pictures○ Ok, maybe cat pictures
  16. 16. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  17. 17. virtualenv● Oh my god use it● Keeps your projects from stepping on eachother● World Peace● Kittens
  18. 18. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  19. 19. Documentation● IMHO lots of docs in the root make a projecthard to navigate● Plaintext format that can generate html docspreferred○ ReStructured Text○ Sphinx
  20. 20. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  21. 21. Project Configuration● Non-sensitive configuration files● Avoid putting these in root, againhierarchical structure
  22. 22. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  23. 23. Newrelic● Makes my life so much better● Monitoring○ Exceptions○ Response Time / Profiling○ Database profiling● Alternatives○ Sentry (FOSS)○ App Dynamics
  24. 24. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  25. 25. Root project urls● If at all possible should only include apps urlpatternsfrom django.conf.urls import patterns, url, includeurlpatterns = patterns(project,url(rachievements/, include(achievements.urls)))
  26. 26. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  27. 27. WSGI● How your webserver actually hooks into yourapp● WSGI middlewares can go here○ Havent ever found a user for them
  28. 28. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  29. 29. Settings● Recently weve started using environmentvariables with some success● Weve also used approaches where we havea settings directory whos __init__.py filedetermins appropriate settings forenvironment● Try to be organized, honestly, just use ENV
  30. 30. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  31. 31. Unpackaged dependencies● Whenever possible use pip / setuptools● Put django snippets / projects that arent inpypi in lib/● If youre going to do something dirty try to beorganized about it
  32. 32. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  33. 33. Project specific Code● As much as possible bundle code intoreusable apps● Things that might have to go here○ Custom test runners○ Project middlewares○ Project template context processors○ Functions that dont make sense elsewhere○ dbrouters
  34. 34. Project StructureREADME.mdrequirements.txtcircle.ymlfabfile.py.gitignorevenv/docs/conf/newrelic.iniproject_name/__init__.pyurls.pywsgi.pysettings/lib/base/apps/
  35. 35. Django Applications● Re-usuable apps make Django awesome○ Use relative imports○ Try to not include anything project specific● Combine / split projects○ Possible with proper organization :)
  36. 36. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  37. 37. Application Tests● You should have tests○ TDD/BDD if possible○ IMHO your model tests / design give better ROI○ If you find a bug write a test to reproduce, fix○ Seriously testing will save you time○ Really○ Please
  38. 38. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  39. 39. Models● Models are the lifeblood of your apps○ Start with model design first○ Design your data model well and views will comenaturally● Model Managers are pretty awesome○ Your views (controllers) should be able to call 1/2model manager methods in order to get what itneeds otherwise code needs to be refactored into amodel manager● Querysets can be subclassed as well○ can make view / task code much much easier○ DRY
  40. 40. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  41. 41. Application Urls● All views accessible in an app should bebundled into an app level urls file● Use named url patterns to separate patternsfrom controllers.
  42. 42. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  43. 43. Django Admin● Major win● Use django admin for as much as you can● Most aspects are subclassable/extentable● Your business guys dont need shiny UI
  44. 44. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  45. 45. Application Views● Make the views as simple as possible○ Template views can probably be functions that callrender○ If a view has lots of model calls that code canprobably go into a manager■ Manager are easier to test■ If managers have lots of interdependencies yourdata model probably needs to be refactored● Use django shortcuts when possible● IMHO template logic is ok to a point if itsimplifies view code, but dont go overboard
  46. 46. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  47. 47. Tasty Pie● Dont re-invent the wheel● Tastypie is useful for most (but not all)RESTful applications● The code is already tested● You dont need as much control over JSONstructure as you think.● This is a separate talk, but be aware of it.
  48. 48. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  49. 49. Background / Async in Django● There are lots of choices for this● ZMQ○ Lots of options for routing○ VERY flexible○ VERY fast○ VERY cool○ Django z-tasks looks promising○ BUT■ Have to write your own workers■ Have to write your own logging / monitoring■ Have to understand a fair amount to make it workcorrectly.■ SO...
  50. 50. Celery● Should be your default choice for async● Very simple python API○ Async tasks○ Tasks in the future○ Cron replacement● Workers are already written● Integrated with new relic○ Ops have been much easier for us○ Traceback monitoring major win
  51. 51. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pyforms.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  52. 52. Django Forms● Model Forms○ DRY○ Handles validation in a declarative way○ Will make your life better● Django crispy forms○ More control
  53. 53. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  54. 54. South● Reproducible migrations○ Schema migrations○ Data migrations○ Test runner○ Probably going to be included in Django 1.6○ Migration dependencies can be defined
  55. 55. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  56. 56. Management Commands● We used to use these for cron jobs○ Celery beat is nicer now● Helpful for project wide scripts that requiredjango context
  57. 57. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  58. 58. Static Files● Namespace your static files○ app_name/static/app_name○ avoids collisions○ common static files can go in the base of the project
  59. 59. App Directory Structureapp_name/tests.pymodels.pyurls.pyadmin.pyviews.pyapi.pytasks.pymigrations/management/__init__.pycommands/command_name.pystatic/app_name/js/css/img/templates/app_name/
  60. 60. Application templates● Same as static files, namespace them● Use template inheritance○ DRY● Use named url patterns in your templates● Dont go overboard with template logic○ If you must check out template includes○ Huge templates are hard to read● Lots of view code data munging can bereplaced with template filters.● Django templates are really controllers○ Let templates act like views, handle presentation.
  61. 61. Other things you should be aware of● Middlewares● Class Based Views● Template Context Processors● Custom Template Tags
  62. 62. Django Apps that make me happy● Django Extensions○ Werkzeug● South○ Probably coming to Django 1.6
  63. 63. Tools you should use● Code Quality○ PEP8○ Pyflakes○ Pychecker○ jshint○ jslint● Development Environment○ ack-grep○ emacs :)● Infrastructure○ PGbouncer
  64. 64. What did I forget?

×