Sunteți pe pagina 1din 12

Django Web Framework

Django is a high­level Python Web framework that 
encourages rapid development and clean, pragmatic 
design
http://www.djangoproject.com

   
Installing Django

In Ubuntu:
sudo apt­get install python­django
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 django­admin
django­admin 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 django­admin to create the app within the project 
directory
django­admin 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

   

S-ar putea să vă placă și