Creating a project
run the command django-admin.py startproject mysite. This will create a mysite directory in your current directory.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.
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!Changing the port
By default, the runserver command starts the development server on 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 8080Full docs for the development server are at django-admin documentation.
Database setup
Now, edit settings.py. It’s a normal Python module with module-level variables representing Django settings. Change these settings to match your database’s connection parameters:
- DATABASE_ENGINE — Either ‘postgresql_psycopg2’, ‘mysql’ or ‘sqlite3’. Other backends are also available.
- DATABASE_NAME — The name of your database, or the full (absolute) path to the database file if you’re using SQLite.
- DATABASE_USER — Your database username (not used for SQLite).
- DATABASE_PASSWORD — Your database password (not used for SQLite).
- DATABASE_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).
sudo aptitude install python-psycopg2
By default, INSTALLED_APPS contains the following apps, all of which come with Django:
- django.contrib.auth — An authentication system.
- django.contrib.contenttypes — A framework for content types.
- django.contrib.sessions — A session framework.
- django.contrib.sites — A framework for managing multiple sites with one Django installation.
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
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.
Creating models
Projects vs. apps
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 configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.
python manage.py startapp polls
Edit the polls/models.py file so it looks like this:
from django.db import models
class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(maxlength=200)
votes = models.IntegerField()
- python manage.py sql polls
- python manage.py validate polls — Checks for any errors in the construction of your models.
- python manage.py sqlcustom polls — Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
- python manage.py sqlclear polls — Outputs the necessary DROP TABLE statements for this app, according to which tables already exist in your database (if any).
- python manage.py sqlindexes polls — Outputs the CREATE INDEX statements for this app.
- python manage.py sqlall polls — A combination of all the SQL from the ‘sql’, ‘sqlcustom’, and ‘sqlindexes’ commands.
Now, run syncdb again to create those model tables in your database:
python manage.py syncdbhttp://www.djangoproject.com/documentation/db-api/
No comments:
Post a Comment