Monday, March 17, 2008

Getting start with Django

I plan to develop our new project Sparrow by Django.Today,we'll do some basic configurations.
Create Project and Config data base.
1) create project
For one thing,of course,we need download the Django and setup it.I decide to grap the last version of Django,so I setup the Subverion client,and then check out the last Django trunk.
svn co http://code.djangoproject.com/svn/django/trunk djtrunk
And then put my djtrunk path to PYTHONPATH environment variables.
finally,we place our djtrunk/django/bin on our system path.because this directary includes management utilities such as django-admin.py.

For now,everything is OK,we'll create our first application in Django environment.It's easy to create a project.Open your django project directory and just type following command.
django-admin.py startproject sparrow
Note:sparrow is our project's name,you can change as everything you like.
After run this command,Django will create a sparrow directory.and this directory include four files:
  • __init__.py: A file required for Python treat the directory as a package (i.e., a group of modules)
  • 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” of your Django-powered site
Now,you have your first Django application.let's test it.Navigate to the sparrow directory and run following command:
python manage.py runserver
you will see some log information,and the server is run on port 8000(default).Then open your browser and open folling url:http://127.0.0.1"8000.You will see a congratulation page with some default legend.Django includes a built-in, lightweight Web server you can use while developing your site. We’ve included this server so you can develop your site rapidly, without having to deal with configuring your production Web server (e.g., Apache) until you’re ready for production.
2) Config data base for Django project.
As of now,Django support three type DataBase:
We plan to use the PostgreSQL.How to do that?
First of all,we'll download the PostgreSQL database engine and download the python postgresql package.
http://www.djangoproject.com/r/python-pgsql/. Take note of whether you’re using version 1 or 2; you’ll need this information later.
Windows user can download from http://www.djangoproject.com/r/python-pgsql/windows/.
Now,we'll config the database in Django.please open the file settings.py in sparrow directoary.
and find the section DATABASE_ENGINE,and change it like this;

DATABASE_ENGINE = 'postgresql_psycopg2' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'test2' # Or path to database file if using sqlite3.
DATABASE_USER = 'postgres' # Not used with sqlite3.
DATABASE_PASSWORD = '123456' # Not used with sqlite3.
DATABASE_HOST = '127.0.0.1' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '5432' # Set to empty string for default. Not used with sqlite3.
Congratulation,we have our first Django application and config it with postgreSQL.It's cool.
In next chapter we'll discuss how to create a Domain model,it'll map to real database table.

No comments: