PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Pycon2009/django/django-tutorial.rst

https://github.com/toastdriven/pydanny-event-notes
ReStructuredText | 331 lines | 277 code | 54 blank | 0 comment | 0 complexity | c61a7a89164479113c070dfbe8716585 MD5 | raw file
  1. =========================
  2. Django in the real world
  3. =========================
  4. By James Bennett and Jacob Kaplan-Moss
  5. * What's on the plate
  6. * testing
  7. * production environments
  8. * deployments
  9. * rest of the web stack
  10. * monitoring
  11. * Performance & tuning
  12. ----
  13. Making your apps reusable for Django
  14. ========================================
  15. * Do one thing and do it well
  16. - Application == encapsulation
  17. - Keep a tight focus
  18. * What does this application do?
  19. * Should just b one or two sentences
  20. * Example: Handle storage of users
  21. * Example: Allow content to be tagged
  22. * Bad Example: Handle entries in a weblog
  23. - Even simple Django sites have a dozen installed apps
  24. - Approach features skeptically
  25. - What does the application do?
  26. * Don't be afraid of multiple apps
  27. - Get rid of the monolithic mindset
  28. * The application is the whole site
  29. * Re-use is an afterthoiught
  30. * Tend to developer apps that are plugins for the master effort
  31. - Django Mindset
  32. * Django encourages multiple applications
  33. * Apps live in the python path so put them anywhere you want
  34. * Abstraction is provided via Site model
  35. - Unrelated features should be in their own apps
  36. - Avoid feature creep
  37. - Orthogonality
  38. * Should be able to change one feature without affecting other areas
  39. * Almost always indicates a the need for a seperate application
  40. - Reuse
  41. * There is no such thing as the single case
  42. - Advantages
  43. * Don't have to keep rewriting the same bits again and again
  44. * Can use old work over and over
  45. * Common Sense
  46. - Form processing
  47. * Supply a form
  48. * But let people specify their own if they want via template parameter in view
  49. * Redirect after successful submission
  50. * Supply a default URL
  51. - URL best practices
  52. * provide a URLConf in the application
  53. * Use named URL patterns
  54. * Use reverse lookup
  55. - Models (egde cases)
  56. * whenever possible, avoid hard-coding a model class
  57. * Use get_model() and take an app labl/model name string instead
  58. * Don't relty on *objects*; use the default manager
  59. - Use to love managers
  60. * Easy to reuse
  61. * Managers are easy to subclass and customize
  62. * Managers let you encapsulate patterns of behavior behind a nice API.
  63. - Advanced techniques
  64. * Encourage subclassing and use of subclasses
  65. * Register things like the admin
  66. * Always consider the API
  67. - Good API design
  68. * Pass in a value to change behavior
  69. * change the value of this setting
  70. - Bad API design
  71. * API? What is that?
  72. * Its open source, just fork it!
  73. * Build to distribute
  74. - Project coupling kills re-use
  75. - Projects should have a settings.py and a URLConf file
  76. - Make your application package-able even if it's only for your own use
  77. - Be up front about your dependencies
  78. - Write for older Python libraries
  79. - Tell people if you are writing against a stable release of Django or not
  80. * Be obsessive about documentation
  81. - It is python: give stuff docstrings
  82. - Do it lots
  83. - then do it more
  84. * Embracing and extending
  85. - Good applications are extensible without hacking them up
  86. - Take advantage of everything the language gives you
  87. - Wrap the code with your own view, this is what decorators are for!
  88. - Relate other models to it
  89. - Write subclasses of it
  90. - You can create proxy subclasses which lets you inherit the code side of things and not the data models.
  91. - Other tricks:
  92. * middleware
  93. * context processrors
  94. - Be a good citizen
  95. * If you change someone else's code, be nice about it
  96. * let the main person know you made the change
  97. ----
  98. Testing
  99. ===========
  100. At first glance looks like it is about code quality and not deployment. But untested code is broken code. The current standard case is Stupidity driven testing, which is writing tests against the stupidity. So when you find a bug, you write a test to duplicate that bug.
  101. Whatever happens, don't let your test suite break thinking, "I'll go back and fix this later."::
  102. Unit Testing <---> Functional Testing <---> Browser
  103. You need all the testing tools and methods. You need to test at multiple level.
  104. * Unit tests
  105. - whitebox testing
  106. - verify the small functional units of your app
  107. - very fine grained
  108. - familiar to many developers
  109. - Django.test.TestCase
  110. * Fixtures
  111. * Test client
  112. * Email capture
  113. * Database management
  114. * Slower than unittest.TestCase
  115. * Doctests
  116. - Easy to read and write
  117. - produces self-documenting code
  118. - Great for cases that only use assetEquals
  119. - Somewhere between unit tests and functional tests
  120. - Difficult to debug
  121. - Doesn't always provide useful test failures
  122. * Functional tests
  123. - a.k.a Behavior driven development
  124. - Blackbox or holistic testing
  125. - Functional testing tools
  126. - Django.test.Client
  127. * Close to browser testing
  128. * Web browser testing
  129. - The ultimate in functional testing for web applications.
  130. - Run test in a web browser
  131. - Can verify JavaScript, AJAX; even design
  132. - Test your site across supported browsers
  133. - Browser Testing Tools
  134. * Selenium
  135. * Windmill (has built-in Django integration)
  136. * Exotic testing
  137. - Static source analysis
  138. - Smoke testing
  139. - Monkey testing
  140. - Load testing
  141. ----
  142. Deployment
  143. =============
  144. Deployment should...
  145. * Be automated.
  146. * Automatically manage dependencies.
  147. * Be isolated.
  148. * Be repeatable.
  149. * Be identicale in staging and production.
  150. * Work the same for everyone.
  151. ===================== =========== ==========
  152. Dependency Management Isolation Automation
  153. ===================== =========== ==========
  154. apt/yum virtualenv capistrano
  155. easy_install zc.buildout fabric
  156. pip puppet
  157. zc.buildout
  158. ===================== =========== ==========
  159. Look into these tools:
  160. * virtualenvwrapper (need to modify my .bashrc)
  161. * pip
  162. ----
  163. Application Servers
  164. ===================
  165. * Apache + mod_python
  166. * Apache + mod_wsgi
  167. * Apache/lighttpd + FastCGI
  168. * SCGI, AJP, nginx/mod_wsgi
  169. Unless you have a really good reason, **use mod_wsgi**. Mod_wsgi is much more predictable than mod_python. Web Faction is a good host.
  170. How does scaling work?
  171. -----------------------
  172. * Scaling means that as your load increases, your system speeds up to match.
  173. Why separate hardware?
  174. * Resource contention
  175. * Separate performance concerns
  176. * 0 -> 1 is much harder than 1 -> N.
  177. Use connection middleware:
  178. * Proxy between web and database layers
  179. * Most implement hot fallover and connection pooling
  180. * Some provide replication, load balancing, parallel queries, connection limiting, etc
  181. * Middleware examples:
  182. * PostgreSQL: pgpool
  183. * MySQL: MySQL: Proxy
  184. * Database-agnostic: sqlrelay
  185. * Oracle: ?
  186. Media server traits:
  187. * Fast
  188. * Lightweight
  189. * Optimized for high concurrency
  190. * Options:
  191. * Apache?
  192. * lighttpd
  193. * nginx
  194. Load balancing:
  195. * Why load balancers?
  196. - So you can have multiple loads
  197. - So you can plug-and-play with new/old systems
  198. * Good traits:
  199. - Low memory overhead
  200. - High concurrency
  201. - Hot fallover
  202. - Other nifty features...
  203. * Examples:
  204. - Apache + mod_proxy (use cautiously)
  205. - perlbal
  206. - nginx
  207. Monitoring
  208. =============
  209. * tell me when my site goes down
  210. * Automatically handle common sources of downtime
  211. * Ideally handle downtime before it even happens
  212. - If load gets heavy, kick in new servers
  213. * Monitor hardware usage to identify hotspots and plan for future growth
  214. * Aid in postmortem analysis
  215. * Give pretty graphics
  216. ----
  217. Availability monitoring principles
  218. ----------------------------------------
  219. * Check services for availability
  220. * More than just ping yoursite.com
  221. * Have some understanding of dependencies
  222. * Notify the right people using the right methods and don't stop until its fixed.
  223. * Minimize false positives
  224. ----
  225. Example tools
  226. --------------
  227. - Internal tools
  228. * Nagios
  229. * Monit
  230. * Zenoss
  231. - External tools
  232. * Pingdom.com
  233. Usage Monitering
  234. -----------------
  235. * keep track of resource usage over time
  236. * Spot and identify trends
  237. * Aid in capacity planning and management
  238. * Usage Monitering tools
  239. - RRDTool
  240. - Munin
  241. - Cacti
  242. - Graphite
  243. Logging and Log Analysis
  244. --------------------------
  245. * print
  246. * Python logging module
  247. * syslogd
  248. * grep | sort | uniq -c | sort -rn
  249. * Look into Splunk
  250. Performance
  251. =============
  252. *When to care about it*
  253. * Ignore performance
  254. * Get the code written
  255. * Make it work
  256. * get it on a server
  257. * then look into it if there is an issue
  258. Django slowdowns:
  259. * Look for code using a lot of query
  260. * Use select_related
  261. * use caching
  262. * Look for complex DB queries and look for ways to simplify them
  263. The DB is the bottleneck or it will be I/O.
  264. * Find out what slow means
  265. * Do testing with tools like wget
  266. * Sometimes the slowness is actually on front end
  267. What to do on the server side:
  268. * Cache
  269. * Cache some more
  270. * Caching turns less hardware into more
  271. * Caching is a trade-off
  272. - Do you want to cache everything from everybody?
  273. - Do you want to only log people logged in?
  274. * Not all views are the same
  275. - Clean up your DB queries
  276. - Use Django's cache middlewhere
  277. * http://www.revsys.com/writings/postgresql-performance.html