Django

来自开放百科 - 灰狐
(版本间的差异)
跳转到: 导航, 搜索
第39行: 第39行:
 
  )
 
  )
 
创建数据库
 
创建数据库
 +
mysql>create database pool
 +
 +
vi settings.py
 +
DATABASE_ENGINE = 'mysql'
 +
DATABASE_NAME = 'poll'
 +
DATABASE_USER = 'root'
 +
DATABASE_PASSWORD = ''
 +
DATABASE_HOST = 'localhost'
 +
DATABASE_PORT = '3306'
 +
 +
python manage.py syncdb
 +
 
  python manage.py sql polls
 
  python manage.py sql polls
 +
 +
BEGIN;
 +
CREATE TABLE `polls_poll` (
 +
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
 +
    `question` varchar(200) NOT NULL,
 +
    `pub_date` datetime NOT NULL
 +
);
 +
CREATE TABLE `polls_choice` (
 +
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
 +
    `poll_id` integer NOT NULL REFERENCES `polls_poll` (`id`),
 +
    `choice` varchar(200) NOT NULL,
 +
    `votes` integer NOT NULL
 +
);
 +
COMMIT;
 +
 +
可看到在mysql中已经创建了相关表
 +
mysql> show tables;
 +
+----------------------------+
 +
| Tables_in_poll            |
 +
+----------------------------+
 +
| 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                |
 +
| polls_choice              |
 +
| polls_poll                |
 +
+----------------------------+
 +
12 rows in set (0.00 sec)
 +
 +
python manage.py shell
 +
>>> from mysite.polls.models import Poll, Choice
 +
>>> Poll.objects.all()
 +
[]
 +
>>> from datetime import datetime
 +
>>> p = Poll(question="What's up?", pub_date=datetime.now())
 +
>>> p.save()
 +
>>> p.id
 +
1L
 +
>>> p.question
 +
"What's up?"
 +
>>> p.pub_date
 +
datetime.datetime(2006, 9, 1, 15, 8, 1, 135114)

2006年9月2日 (六) 12:15的版本

Django: The Web framework for perfectionists with deadlines

安装指南

First, download Django-0.95.tar.gz. Then:

tar xzvf Django-0.95.tar.gz
cd Django-0.95
sudo python setup.py install
cd /var/www
django-admin.py startproject mysite
cd mysite
python manage.py runserver
http://localhost:8000

创建一个poll应用

python manage.py startapp polls
vi polls/models.py
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()
vi polls/settings.py
INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'mysite.polls'
)

创建数据库

mysql>create database pool
vi settings.py
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'poll'
DATABASE_USER = 'root'
DATABASE_PASSWORD = 
DATABASE_HOST = 'localhost'
DATABASE_PORT = '3306'
python manage.py syncdb

python manage.py sql polls
BEGIN;
CREATE TABLE `polls_poll` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `question` varchar(200) NOT NULL,
    `pub_date` datetime NOT NULL
);
CREATE TABLE `polls_choice` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `poll_id` integer NOT NULL REFERENCES `polls_poll` (`id`),
    `choice` varchar(200) NOT NULL,
    `votes` integer NOT NULL
);
COMMIT;

可看到在mysql中已经创建了相关表

mysql> show tables;
+----------------------------+
| Tables_in_poll             |
+----------------------------+
| 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                |
| polls_choice               |
| polls_poll                 |
+----------------------------+
12 rows in set (0.00 sec)
python manage.py shell
>>> from mysite.polls.models import Poll, Choice
>>> Poll.objects.all()
[]
>>> from datetime import datetime
>>> p = Poll(question="What's up?", pub_date=datetime.now())
>>> p.save()
>>> p.id
1L
>>> p.question
"What's up?"
>>> p.pub_date
datetime.datetime(2006, 9, 1, 15, 8, 1, 135114)
分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱