Introduction to Django
Upcoming SlideShare
Loading in...5
×
 

Introduction to Django

on

  • 558 views

Most of the code examples here were from the official Django tutorial. So for more detail go to:

Most of the code examples here were from the official Django tutorial. So for more detail go to:

Statistics

Views

Total Views
558
Views on SlideShare
535
Embed Views
23

Actions

Likes
1
Downloads
28
Comments
0

1 Embed 23

http://www.meetup.com 23

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Introduction to Django Introduction to Django Presentation Transcript

    • Django is a high-level Python Web framework that encourages rapid developmentand clean, pragmatic design.True! (it enocourages, doesnt force you) ● Object Relational Mapper ● Automatic Admin Interface ● URL dispatcher ● Template System ● Cache System ● Internationalisation ● Forms, with data validatio0n ● devlopment web server
    • Django - overview Typical scenario urls.py views.py request models.py request webclient server response Database response (normally html)
    • models.py● This is the file that defines the objects / Poll Choice ralationships. Django equivalent of SQL create table commands.● Can also define instance methods – from django.db import models that act on one class Poll(models.Model): instance of the object. question = models.CharField(max_length=200) pub_date = models.DateTimeField(date published) class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
    • models.py Poll Choicefrom django.db import modelsclass Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField(date published)class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)[user]$ python manage.py synchdb BEGIN; CREATE TABLE "polls_poll" ( "id" serial NOT NULL PRIMARY KEY, "question" varchar(200) NOT NULL, "pub_date" timestamp with time zone NOT NULL ); CREATE TABLE "polls_choice" ( "id" serial NOT NULL PRIMARY KEY, "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL ); COMMIT;
    • Now we have an API to play with! This saves writing a lot of boiler plate SQL code, e.g. [user]$ python manage.py shell INSERTs, UPDATEs, etc>>> from polls.models import Poll, Choice # Import the model classes we just wrote.# No polls are in the system yet.>>> Poll.objects.all()[]# Create a new Poll.>>> from django.utils import timezone>>> p = Poll(question="Whats new?", pub_date=timezone.now())>>> p.save()# Now it has an ID. Note that this might say "1L" instead of "1", depending# on which database youre using. Thats no biggie; it just means your# database backend prefers to return integers as Python long integer# objects.>>> p.id1# Access database columns via Python attributes.>>> p.question"Whats new?">>> p.pub_datedatetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)# Change values by changing the attributes, then calling save().>>> p.question = "Whats up?">>> p.save()# objects.all() displays all the polls in the database.>>> Poll.objects.all()[<Poll: Poll object>]
    • Admin Interface“One of the most powerful parts of Django is the automatic admin interface. It reads metadatain your model to provide a powerful and production-ready interface that content producers canimmediately use to start adding content to the site. In this document, we discuss how toactivate, use and customize Django’s admin interface.” ● One of Djangos best features. ● Provides a web page to view / enter / update data, one per model (one per database table). ● Automatically generated. ● Lots of configuartion options. ● Probably able to do a large chunk of what you want to do (for little effort)
    • admin.pyfrom polls.models import Poll, Choicefrom django.contrib import adminclass ChoiceInline(admin.TabularInline): model = Choice extra = 3class PollAdmin(admin.ModelAdmin): fieldsets = [ (None, {fields: [question]}), (Date information, {fields: [pub_date], classes: [collapse]}), ] list_display = (question, pub_date, was_published_today) list_filter = [pub_date] search_fields = [question] date_hierarchy = pub_date inlines = [ChoiceInline]admin.site.register(Poll, PollAdmin)
    • Django - overview urls.py views.py request models.py request webclient server response Database response (normally html)
    • urls.py ● This is the file that tells django what code to execute based on the url ● Uses regular expressions, and passes caturing groups to the viewfrom django.conf.urls.defaults import *from django.conf.urls.static import staticfrom django.conf import settingsfrom sequencing import viewsfrom django.contrib import adminadmin.autodiscover()urlpatterns = patterns(, (r^admin/doc/, include(django.contrib.admindocs.urls)), (radmin/sequencing/lookup_values/(?P<table>S+)/(?P<field>S+)/ , sequencing.views.lookup_values), # goes to lookup_values # in views.py, with paramters, # self, table, and field (r^admin/dates_view/ , DatesView.as_view()) , # example of a generic list view)
    • views.py● So each view will take an HTTP request object, and return a HTTP response.● Usually you will return HTML, but other responses such as JSON or excel can be returned. A very basic example:from django.http import HttpResponsedef index(request): return HttpResponse("Hello, world. Youre at the poll index.")
    • views.py and models.py togetherfrom django.http import HttpResponsefrom polls.models import Poll # importing Poll object from models.pydef index(request): latest_poll_list = Poll.objects.order_by(-pub_date)[:5] output = , .join([p.question for p in latest_poll_list]) return HttpResponse(output) This line will generate SQL equivalent to: SELECT * FROM myapp__poll ORDER BY pub_date DESC LIMIT 5
    • Django - overview A view usually generates a queryset urls.py (from models), and views.py passes it to a template to generate a request models.py response request webclient server response Database (normally html) response
    • views using a templateviews.pyfrom django.http import HttpResponsefrom django.template import Context, loaderfrom polls.models import Polldef index(request): latest_poll_list = Poll.objects.order_by(-pub_date)[:5] template = loader.get_template(polls/index.html) context = Context({ latest_poll_list: latest_poll_list, }) return HttpResponse(template.render(context))polls/index.html{% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> {% endfor %} </ul>{% else %} <p>No polls are available.</p>{% endif %}
    • Good Bad● Very well doccumented ● ORM is somehwat limted. For example no contidtional agrregates.● Well known, lots of answers on Stack Overflow ● Template system is somewhat imited, no logic in templates.● Lots of third party apps (plugins), e.g Tastypie for REST, Django Debug ● These things can be replaced, but then Toolbar, CMS pugins you loose a lot of the magic that makes it so great. For example replacing the● (Semi) automatically generated Admin ORM with SQL alchemy is possible, but interface. May do most of what you need then you loose the Admin interface. for very little effort (just configuration). ● Schema changes are a bit of a pain (though to be fair, probably the same for any DB application). South application helps here. so-so ● Deployment is not as easy as PHP, but there are a few options. ● Hosting not as ubiqutous. Though this is true for any Python framework