2. What is Django ?
● Django is a high-level Python Web framework
that encourages rapid development and clean,
pragmatic design.
● Django makes it easier to build better Web apps
more quickly and with less code.
● The Web framework with perfectionists with
deadlines.
4. Features
● Object- Relation ● Unicode support
Mapper ● Cache framework
● Templating Language ● Testing framework
● Automatic Language ● Great docs (650+pages
● Elegant ursl ● Friendly community
● MVC architecture
5. More Features
● Jython support ● Send emails easily
● Nice support for forms ● Built -in site maps
● Built in dcv server ● Built-in RSS/ATOM
● Solid security ● ”Signal” hooks
emphasis
6. Components
● Models – Django ORM
● Templates - Django Templates Engine
● Views – Python function , Request in , Request
out
● URL Patterns – Regular expression based
8. Run Server
$ ./manage.py runserver
Validating models...
0 errors found.
Django version 1.2-pre, using settings
'myproject.settings'
Development server is running at
http://127.0.0.1:8000/
Quit the server with CONTROL-C.
12. Edit polls/model.py
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
13. Continue ...
● $python manage.py sql polls
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"),
"choice" varchar(200) NOT NULL,
"votes" integer NOT NULL);
COMMIT;
● $python manage.py syncdb // update database
14. Playing With API
>>> from mysite.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.
>>> import datetime
>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())
# Save the object into the database. You have to call save() explicitly.
>>> p.save()
# Access database columns via Python attributes.
>>> p.question
"What's up?"
>>> p.pub_date
datetime.datetime(2007, 7, 15, 12, 00, 53)
# Change values by changing the attributes, then calling save().
>>> p.pub_date = datetime.datetime(2007, 4, 1, 0, 0)
>>> p.save()
# objects.all() displays all the polls in the database.
>>> Poll.objects.all()
[<Poll: Poll object>]
15. Activate the admin site
Edit the file /mysite/urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below and add
'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
16. Start the development server
$python manage.py runserver
Now, open a Web browser and go to "/admin/" on your local domain --
e.g., http://127.0.0.1:8000/admin/. You should see the admin's login
screen: