first django app part 1

file:///home/bigzhu/django/docs/_build/html/index.html

建立项目

django-admin.py startproject mysite

启动 development server

默认用 8000 端口,可我己经用来作 gfw 代理了.

cd mysite
python manage.py runserver 8001

设数据库

应用得有数据库吧.

gvim settings.py 

n 多的配置都在这个文件里设.

我准备用下 postgresql ,所以按这个数据库来设,其它数据库的自己设

找到

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', * Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      * Or path to database file if using sqlite3.
        'USER': '',                      * Not used with sqlite3.
        'PASSWORD': '',                  * Not used with sqlite3.
        'HOST': '',                      * Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      * Set to empty string for default. Not used with sqlite3.
    }
}

改为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', * Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'tel_me_site',                      * Or path to database file if using sqlite3.
        'USER': 'bigzhu',                      * Not used with sqlite3.
        'PASSWORD': '123',                  * Not used with sqlite3.
        'HOST': '127.0.0.1',                      * Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      * Set to empty string for default. Not used with sqlite3.
    }
}

初始化数据库

设好了以后,在 project 下执行以下命令,初始化数据库:

python manage.py syncdb

同时会让你建立一个超级用户

建立 apps

什么是 app 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.

在 project 下执行:

python manage.py startapp polls

这样就建立了一个名叫 polls 的 app,其实就是一个 polls 目录,里面有这些文件:

polls/
    __init__.py
    models.py
    tests.py
    views.py

下面来学这些怎么用了

通过 models 实现orm

通过 models 实现orm:

cd polls
gvim models.py

加入以下內容:

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

这两个 class 一看就知道,是定义了两张表.

这两张表怎么建到数据库里呢? 先要注册 app

注册 app

cd ..
gvim settings.py

找到 INSTALLED_APPS,把刚才建的 polls,加进去:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    * Uncomment the next line to enable the admin:
    * 'django.contrib.admin',
    * Uncomment the next line to enable admin documentation:
    * 'django.contrib.admindocs',
)

orm 提交数据库

先执行

python manage.py sql polls

app pools 的 model 会执行的 sql 会被打出来(并没有执行):

BEGIN;
CREATE TABLE "polls_poll" (
    "id" serial NOT NULL PRIMARY KEY,
    "question" varchar(200) NOT NULL,
    "pub_date" timestamp with time zone NOT NULL
)
;
CREATE TABLE "polls_choice" (
    "id" serial NOT NULL PRIMARY KEY,
    "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED,
    "choice" varchar(200) NOT NULL,
    "votes" integer NOT NULL
)
;
COMMIT;

还是执行这个:

python manage.py syncdb

表会被建到数据库中,这个命令是不是有点熟?

没错,初始化数据库时用的也是这个命令.

修改数据模型

first django app part 2