Django For Beginners
Django For Beginners
Django is a highlevel Python Web framework that
encourages rapid development and clean, pragmatic
design
http://www.djangoproject.com
Installing Django
In Ubuntu:
sudo aptget install pythondjango
From Subversion
svn co http://code.djangoproject.com/svn/django/trunk
Your first project
Create a directory for your projects
mkdir /home/USERNAME/Django
Start the project using djangoadmin
djangoadmin startproject mysite
CD to the directory, run the server
cd mysite; python manage.py runserver
View the site at http://localhost:8000/
What you should see...
Your first app
Use djangoadmin to create the app within the project
directory
djangoadmin startapp bglug
CD to the app, edit views.py
Your first app (continued)
CD up one directory, edit urls.py
Your first app (results)
Run the server again
python manage.py runserver
Templating
Edit your settings.py file to include your template
directory and media root
Edit your urls.py to include your media directory (for
CSS, images, javascript, etc).
Only use /media dir in Debug mode
Base.html
Like a master page, goes in your templates directory
All other pages can ”extend” from it
Views and Templates
To use a template with a view, you have to import
different modules
from django.shortcuts import render_to_response
Any variables you use inside the view function can be
passed to the template
Models and Admin Interface
Edit settings.py, add DB settings (sqlite3 for now) and
admin to installed apps
CD to your app, edit models.py
Edit urls.py to add admin interface
python manage.py syncdb to create DB structure and
superuser account
Your admin interface
Include models in views
Back in views.py, add Modelname.objects.all() to get
all items in the database of type ”Modelname”
Add ”locals()” to the return statement of the view to
return all local variables
In the template, use a for loop to cycle through items
in the database