Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword or section
Like this
6Activity

Table Of Contents

0 of .
Results for:
No results containing your search query
P. 1
django

django

Ratings: (0)|Views: 150 |Likes:
Published by evaneglineeleanor

More info:

Published by: evaneglineeleanor on Aug 03, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/28/2011

pdf

text

original

 
Django
Dejan Lukan
evangeline.eleanor@gmail.com
August 3, 2010
1
 
1 Intoduction
1.1 Creating a project
If this is the first time you’re using django, you have to take care of initial setup (auto-generate some code thatestablishes a django project):
#
cd to the directory where you would like to store your codecd
<
dir
>
#
create
new
projectdjango
admin .py startproject
<
project name
>
If your background is in PHP, you’re probably used to putting code under the web server’s document root(/var/www/). With django, you don’t do that - it’s not a good idea to put any of this python code wihin yourweb server’s document root, because it risks the possibility that people may be able to view your code over theweb. Put your code in some directory outside of the document root.Startproject created:
rw
r
r
1 eleanor eleanor 0 Mar 7 15:08 i n i t .py
rw
r
r
1 eleanor eleanor 546 Mar 7 15:08 manage.py
rw
r
r
1 eleanor eleanor 2773 Mar 7 15:08 settings .py
rw
r
r
1 eleanor eleanor 542 Mar 7 15:08 urls .py
Explanation:
init .py: empty file that tells python that this directory should be considered a python package
manage.py: a command line utility that lets you interact with this django project in various ways
settings.py: Settings/configuration for this django project
urls.py: the url declarations for this django project (a table of contents)
1.2 The development server
Let’s verify that this worked. Change into the
<
dir
>
directory and run:
$ python manage.py runserverValidating models . . .0 errors foundDjango version 1.1.1 ,
using
settings estudent . settings ’Development server is running at http :
//127.0.0.1:8000/ 
Quit the server with CONTROL
C.
you’ve started the django development server, a lightweight web server written purely in python (you don’thave to bother yourself with apache).
1.3 Changing the ip + port
By default, the runserver command starts the development server on the internal IP at port 8000. If you wantto change the server’s port, pass it as a command-line argument:
$ python manage.py runserver 8080
If you want to change the server’s ip, pass it along with the port (to listen on public IPs use):
python manage.py runserver 0.0.0.0:8000
1
 
1.4 Database setup
Edit setings.py:
DATABASE ENGINE: [postgresql psycopg2, mysql, sqlite3]
DATABASE NAME: the name of your database (if using sqlite the database will be a file in your computer:a full absolute path. If the file doesn’t exist it will automatically be created when you sync the database forthe first time). If you’re using mysql create the database with ”CREATE DATABASE
<
database name
>
.
DATABASE USER: your database user (not used in sqlite)
DATABASE PASSWORD: your database password (not used in sqlite)
DATABASE HOST: the host your database is on (empty string if database is on the same pysical machine- not used for sqlite)
INSTALLED APPS: the names of all django applications that are activated in this django instance. Bydefault it contains these apps:
django.contrib.auth - an authentication system
djando.contrib.contenttypes - a framework for content types
djando.contrib.sessions - a session framework
django.contrib.sites - a framework for managing multiple sites with one django installationEach of these applications makes use of at least one database table, so we need to create the tables in thedatabase before we can use them:
python manage.py syncdb
this looks at the INSTALLED APPS setting and creates any necessary database tables according to thedatabase settings in your settings.py file.Test if the database is setup correctly (start the shell):
>>>
from django .db import connection
>>>
cursor = connection . cursor ()
>>>
1.5 Creating models
Now that your environment is set up, you can start doing work.Each application you write in django consists of a python package, somewhere on your python path. Djangocomes with a utility that automatically generates the basic directory structure of an app, so you can focus onwriting code rather than creating directories.What’s the difference between a project and an app? An app is a Web application that does something e.g., a weblog system, a database of public records or a simple poll app. A project is a collection of configura-tion and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.To create your app, make sure you’re in the mysite directory and type this:
python manage.py startapp
<
app name
>
2

Activity (6)

You've already reviewed this. Edit your review.
1 hundred reads
Hanse69 liked this
Hanse69 liked this
Carmel Mount liked this
James Mwai liked this
pdfsscribd liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->