Django introduction   Kevin Van Wilder      www.inuits.eu
Topics• What is Django?• Framework overview• Getting started
Who uses Django?•   Pinterest           •   New York Times•   Instagram           •   Washington Post•   Openstack        ...
What is Django?• Not a CMS!• Web application framework to create  complex, dynamic and datadriven websites  with emphasis ...
Features• Admin interface (CRUD)• Templating• Form handling• Internationalisation (i18n)• Sessions, User management, role-...
FRAMEWORK OVERVIEW
Models ViewsTemplates                   Application Models ViewsTemplates                   Application                   ...
Model, View, Template• Model  – Abstracts data by defining classes for them and    stores them in relational tables• View ...
Framework
URLConf• URLs are matched via patterns and mapped  View functions/classes that create the output.    urlpatterns = pattern...
Views• Receives a request and generates a response• Interacts with:   – Models   – Templates   – Caching• Mostly fits insi...
Views (continued)• Just rendering text:  – TemplateView• If that doesn’t float your boat: Mixins  – SingleObjectMixin  – F...
Overviewurlpatterns = patterns(,    url(r^books/([w-]+)/$,        PublisherBookList.as_view(),        name=author-detail),...
Templates{% extends "base_generic.html" %}{% block title %}    {{ publisher.name }}{% endblock %}{% block content %}    <h...
Models• Abstract representation of data• Database independent• Object oriented (methods, inheritance, etc)
from django.db import modelsclass Publisher(models.Model):    name = models.CharField(max_length=30)    address = models.C...
Querying the ORMBook.objects.get(title=“On the Origin of Species”)SELECT *FROM booksWHERE title = “On the Origin of specie...
Querying the ORMBooks.objects  .filter(genre=“drama”)  .order_by(“-publish_date”)  .annotate(author_count=Count(‘author’))...
GETTING STARTED
Working on “Onderwijstips”# SETTING UP ENVIRONMENT$ mkvirtualenv onderwijstips#   CHECKING OUT THE CODE$   git clone git@g...
Git Repository• Git Flow (http://nvie.com/posts/a-successful-git-branching-model/)   – master       • Always tagged with a...
How are we deploying?• Configuration:  – Apache2 + mod_wsgi  – MySQL  – Optimize only when necessary    (varnish, memcache...
Onderwijstips• Three applications   – Tips (front-end for users)   – Editors (back-end for tip author)   – Migration (plom...
DEMO TIME!
Django introduction @ UGent
Upcoming SlideShare
Loading in...5
×

Django introduction @ UGent

422

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
422
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Django introduction @ UGent

  1. 1. Django introduction Kevin Van Wilder www.inuits.eu
  2. 2. Topics• What is Django?• Framework overview• Getting started
  3. 3. Who uses Django?• Pinterest • New York Times• Instagram • Washington Post• Openstack • The Guardian• Disqus • Mercedes Benz• Firefox • National Geographic• NASA • Discovery Channel• Bitbucket • Orange• The Onion • ...
  4. 4. What is Django?• Not a CMS!• Web application framework to create complex, dynamic and datadriven websites with emphasis on: – Loose coupling and tight cohesion – Less code – Quick development – DRY – Consistent
  5. 5. Features• Admin interface (CRUD)• Templating• Form handling• Internationalisation (i18n)• Sessions, User management, role-based permissions.• Object-Relational Mapping (ORM)• Testing Framework• Fantastic documentation
  6. 6. FRAMEWORK OVERVIEW
  7. 7. Models ViewsTemplates Application Models ViewsTemplates Application Site/Project Project Structure Settings Urls Templates
  8. 8. Model, View, Template• Model – Abstracts data by defining classes for them and stores them in relational tables• View – Takes a browser request and generates what the user gets to see• Template – Defines how the user will see the view
  9. 9. Framework
  10. 10. URLConf• URLs are matched via patterns and mapped View functions/classes that create the output. urlpatterns = patterns(, url(r^$, TipListView.as_view(), name=tip-list), url(r^tips/(?P<slug>[-w]+)/$, TipDetailView.as_view(), name=tip-detail), )
  11. 11. Views• Receives a request and generates a response• Interacts with: – Models – Templates – Caching• Mostly fits inside one of the following categories: – Show a list of objects – Show details of an object – Create/Update/Delete an object – Show objects by year, month, week, day...
  12. 12. Views (continued)• Just rendering text: – TemplateView• If that doesn’t float your boat: Mixins – SingleObjectMixin – FormMixin – ModelFormMixin – ...
  13. 13. Overviewurlpatterns = patterns(, url(r^books/([w-]+)/$, PublisherBookList.as_view(), name=author-detail),)class PublisherBookList(ListView): template_name = books/books_by_publisher.html def get_queryset(self): self.publisher = get_object_or_404(Publisher, name=self.args[0]) return Book.objects.filter(publisher=self.publisher)
  14. 14. Templates{% extends "base_generic.html" %}{% block title %} {{ publisher.name }}{% endblock %}{% block content %} <h1>{{ publisher.name }}</h1> {% for book in object_list %} <h2> <a href="{{ book.get_absolute_url }}"> {{ book.title }} </a> </h2> {% endfor %}{% endblock %}
  15. 15. Models• Abstract representation of data• Database independent• Object oriented (methods, inheritance, etc)
  16. 16. from django.db import modelsclass Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField() class Meta: ordering = ["-name"] def __unicode__(self): return self.nameclass Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField()
  17. 17. Querying the ORMBook.objects.get(title=“On the Origin of Species”)SELECT *FROM booksWHERE title = “On the Origin of species”;
  18. 18. Querying the ORMBooks.objects .filter(genre=“drama”) .order_by(“-publish_date”) .annotate(author_count=Count(‘author’)) .only(“title”, “publish_date”, “genre”)
  19. 19. GETTING STARTED
  20. 20. Working on “Onderwijstips”# SETTING UP ENVIRONMENT$ mkvirtualenv onderwijstips# CHECKING OUT THE CODE$ git clone git@github.ugent.be:Portaal/site-onderwijstips.git$ cd site-onderwijstips$ git checkout develop# BUILDING$ ln –s buildout_dev.cfg buildout.cfg$ python bootstrap.py$ bin/buildout –vv # uses private github projects, requires permissions# STARTING DJANGO$ bin/django migrate$ bin/django runserver
  21. 21. Git Repository• Git Flow (http://nvie.com/posts/a-successful-git-branching-model/) – master • Always tagged with a version • To be deployed – develop • Next version currently in development – release/1.x.x • A release being prepared • Merges into master with tag 1.x.x
  22. 22. How are we deploying?• Configuration: – Apache2 + mod_wsgi – MySQL – Optimize only when necessary (varnish, memcache, ...)• Jenkins pipeline – Perform a buildout – Generate .deb file – Upload to Infrastructure Package Repository – Triggers a Puppet run
  23. 23. Onderwijstips• Three applications – Tips (front-end for users) – Editors (back-end for tip author) – Migration (plomino importer)• Features: – Haystack search indexing (django-haystack) – User authentication (django-cas) – MediaMosa Integration (django-mediamosa) – Tagging (django-taggit) – Ugent Theming (django-ugent)• Buildout (Thx Bert!)• Monitoring via Sentry• Database schema migration management via South
  24. 24. DEMO TIME!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×