(一)创建项目
django-admin.py startproject mysite
运行这个命令时,需要django-admin.py在PATH环境变量中
我的django-admin.py在 Django目录\Django-1.4.1\django\bin中
此时目录结构为
mysite
│ manage.py
│
├─blog
│ models.py
│ tests.py
│ views.py
│ __init__.py
│
└─mysite
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
__init__.py
__init__.pyc
(二)创建项目blog
manage.py startapp blog
(三)设置model
找到models.py
from django.db import models
#Create your models here
删掉注释,加入以下代码
class BlogPost(model.Model):
title=models.CharField(max_length=150)
body=models.TextField()
timestamp=models.DateTimeField()
(四)设置数据库
(1)生成数据库
我们使用MYSQL,用命令Create Database djangodb;生成数据库djangodb
(2)设置配置文件
打开配置文件setting.py,找到DATABASES ={...},改成
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djangodb', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'djcsch2001', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
(3)生成表
manage.py syncdb
生成表后会提示是否生成admin账户:
You just installed Django's auth system,which means you don't hava any superusers definde.
Would you like to create one now?(yes/no):yes
选择yes,并输入用户名和密码即可