Creating a project If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.
From the command line, cd into a directory where you’d like to store your
code, then run the following command:
django-admin startproject prj
it will create a prj directory in the current directory.there are some files in the prj directory: [root@localhost prj]# ls __init__.py manage.py settings.py urls.py
These files are:
- __init__.py: An empty file that tells Python that this directory
should be considered a Python package. (Read more about packages in the
official Python docs if you're a Python beginner.)
- manage.py: A command-line utility that lets you interact with this
Django project in various ways. You can read all the details about
manage.py in django-admin.py and manage.py.
- settings.py: Settings/configuration for this Django project.
Django settings will tell you all about how settings work.
- urls.py: The URL declarations for this Django project; a "table of
contents" of your Django-powered site. You can read more about URLs in
URL dispatcher.
The development server
Let's verify this worked. Change into the mysite directory, if you
haven't already, and run the command python manage.py runserver. You'll see
the following output on the command line:
Validating models...
0 errors found.
Django version 1.0, using settings 'prj.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. We've included this with Django so you can develop things
rapidly, without having to deal with configuring a production server -- such as
Apache -- until you're ready for production.
Now's a good time to note: DON'T use this server in anything resembling a
production environment. It's intended only for use while developing. (We're in
the business of making Web frameworks, not Web servers.)
Now that the server's running, visit http://127.0.0.1:8000/ with your Web
browser. You'll see a "Welcome to Django" page, in pleasant, light-blue pastel.
It worked!
By default, the runserver command starts the development server
on the internal IP at port 8000.
If you want to change the server's port, pass
it as a command-line argument. For instance, this command starts the server
on port 8080:
python manage.py runserver 8080
If you want to change the server's IP, pass it along with the port. So to
listen on all public IPs (useful if you want to show off your work on other
computers), use:
python manage.py runserver 0.0.0.0:8000
Full docs for the development server can be found in the
runserver reference.
Database setup
Now, edit settings.py. It's a normal Python module with
module-level variables representing Django settings. Change the
following keys in the DATABASES 'default' item to match
your databases connection settings.
ENGINE -- Either
'django.db.backends.postgresql_psycopg2',
'django.db.backends.mysql' or
'django.db.backends.sqlite3'. Other backends are
also available.
NAME -- The name of your database. If you're using
SQLite, the database will be a file on your computer; in that
case, NAME should be the full absolute path,
including filename, of that file. If the file doesn't exist, it
will automatically be created when you synchronize the database
for the first time (see below).
When specifying the path, always use forward slashes, even on
Windows (e.g. C:/homes/user/mysite/sqlite3.db).
USER -- Your database username (not used for SQLite).
PASSWORD -- Your database password (not used for
SQLite).
HOST -- The host your database is on. Leave this as
an empty string if your database server is on the same physical
machine (not used for SQLite).
If you're new to databases, we recommend simply using SQLite (by
setting ENGINE to 'django.db.backends.sqlite3'). SQLite
is included as part of Python 2.5 and later, so you won't need to
install anything else.
Note
If you're using PostgreSQL or MySQL, make sure you've created a database by
this point. Do that with "CREATE DATABASE database_name;" within your
database's interactive prompt.
If you're using SQLite, you don't need to create anything beforehand - the
database file will be created automatically when it is needed.
While you're editing settings.py, take note of the
INSTALLED_APPS setting towards the bottom of the file. That variable
holds the names of all Django applications that are activated in this Django
instance. Apps can be used in multiple projects, and you can package and
distribute them for use by others in their projects.
By default, INSTALLED_APPS contains the following apps, all of which
come with Django:
These applications are included by default as a convenience for the common case.
Each of these applications makes use of at least one database table, though,
so we need to create the tables in the database before we can use them. To do
that, run the following command:
python manage.py syncdb
The syncdb command looks at the INSTALLED_APPS setting and
creates any necessary database tables according to the database settings in your
settings.py file. You'll see a message for each database table it
creates, and you'll get a prompt asking you if you'd like to create a superuser
account for the authentication system. Go ahead and do that.
If you're interested, run the command-line client for your database and type
\dt (PostgreSQL), SHOW TABLES; (MySQL), or .schema (SQLite) to
display the tables Django created.
For the minimalists
Like we said above, the default applications are included for the common
case, but not everybody needs them. If you don't need any or all of them,
feel free to comment-out or delete the appropriate line(s) from
INSTALLED_APPS before running syncdb. The
syncdb command will only create tables for apps in
INSTALLED_APPS. INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', )
+----------------------------+ | Tables_in_django | +----------------------------+ | auth_group | | auth_group_permissions | | auth_message | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_content_type | | django_session | | django_site | +----------------------------+
|