Django tech-talk

637 views

Published on

Simple overview of the Django Web Framework mainly discussing the MVC pattern and templates.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
637
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Django tech-talk

  1. 1. Tech Talk: Django A Brief Overview and Examples Dustin Dannenhauer dtdannen @ indiana.edu March 22nd, 2011
  2. 2. What is Django? <ul><li>Development tool that makes building websites easier
  3. 3. Written in Python
  4. 4. DRY (Don't Repeat Yourself)
  5. 5. Originally designed for news websites </li></ul>
  6. 6. Current sites using Django <ul><li>bitbucket.org
  7. 7. guardian.co.uk (~2.5 million hits a month)
  8. 8. giantbomb.com
  9. 9. newyorktimes.com
  10. 10. comicvine.com
  11. 11. washingtonpost.com
  12. 12. lawrence.com </li></ul>
  13. 13. Why use a web framework? <ul><li>Make things easier </li><ul><li>Database ORM's
  14. 14. Automatic Admin Web Interface
  15. 15. Elegant URL design </li></ul><li>Remove Redundancy </li><ul><li>Template System (don't want to copy html around like headers, footers, etc)
  16. 16. Model-View-Controller architectural pattern (MVC) </li></ul></ul>
  17. 17. Model-View-Controller(MVC) <ul><li>Model = Database
  18. 18. View = User Interface
  19. 19. Controller = Handles Input/Output </li></ul>
  20. 20. Model = Database ORM <ul><li>Object Relational Mapper (ORM)
  21. 21. Instead of writing SQL statements to interact with the database, use objects (classes) in python to interact with relational database </li></ul>
  22. 22. Model Details <ul><li>Transfer data between the objects (Python classes) and the relational database
  23. 23. Each Python class represents a table in the database, each actual object is a row
  24. 24. Python classes defined in file: models.py
  25. 25. All fields of a class represent a field in the database
  26. 26. You can add your own methods on top to supplement fields </li></ul>
  27. 27. Example: models.py from django.db import models class Reporter(models.Model): full_name = models.CharField(max_length=70) class Article(models.Model): pub_date = models.DateTimeField() headline = models.CharField(max_length=200) content = models.TextField() reporter = models.ForeignKey(Reporter)
  28. 28. Using classes in models.py >>> Reporter.objects.all() [] # Create a new Reporter. >>> r = Reporter(full_name='John Smith') # Save the object into the database. You have to call save() explicitly. >>> r.save() # Now it has an ID. >>> r.id 1 # Now the new reporter is in the database. >>> Reporter.objects.all() [<Reporter: John Smith>]
  29. 29. View <ul><li>Python functions that are called when a user goes to a specific url </li><ul><li>Ex: www.site.com/login calls the view mapped to login </li></ul><li>Every URL is mapped to a view
  30. 30. Code is written in file: views.py </li></ul>
  31. 31. Urls <ul><li>Every URL has a corresponding view
  32. 32. Data from the URL can be passed into the view
  33. 33. Uses regular expressions to match the URL to what the user tried to go to </li></ul>
  34. 34. Elegant Urls <ul><li>When a user visits a url, the connected view is called with any data from the url
  35. 35. urlpatterns = patterns('',
  36. 36. (r'^articles/(d{4})$','news.views.year_archive'),
  37. 37. (r'^articles/(d{4})/(d{2})/$',
  38. 38. 'news.views.month_archive'),
  39. 39. (r'^articles/(d{4})/(d{2})/(d+)/$', </li><ul><li>'news.views.article_detail'), </li></ul><li>) </li></ul>
  40. 40. Example: views.py def year_archive(request, year): a_list = Article.objects.filter(pub_date__year=year) return render_to_response( 'news/year_archive.html', {'year': year, 'article_list': a_list})
  41. 41. Templates <ul><li>The result of a view function is ”roughly” the html page returned to the user
  42. 42. Variables can be passed into a template
  43. 43. Template Inheritance </li></ul>
  44. 44. Example Base Template <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <img src=&quot;sitelogo.gif&quot; alt=&quot;Logo&quot; /> {% block content %}{% endblock %} </body> </html>
  45. 45. Example Child Template {% extends &quot;base.html&quot; %} {% block title %}Articles for {{ year }}{% endblock %} {% block content %} <h1>Articles for {{ year }}</h1> {% for article in article_list %} <p>{{ article.headline }}</p> <p>By {{ article.reporter.full_name }}</p> <p>Published {{ article.pub_date|date:&quot;F j, Y&quot; }}</p> {% endfor %} {% endblock %}
  46. 46. Not Mentioned <ul><li>Caching Framework
  47. 47. Syndication Framework (RSS and Atom Feeds)
  48. 48. Modularity of Django (many little apps)
  49. 49. Deployment
  50. 50. and much more... </li></ul>
  51. 51. More Resources <ul><li>http://www.djangoproject.com/
  52. 52. http://www.djangobook.com/
  53. 53. Presentation on ”High Performance Django”
  54. 54. http://www.slideshare.net/zeeg/django-con-high-performance-django-presentation </li></ul>

×