一、在使用Django框架之前,首先要搭建Django开发环境。(首先要保证电脑上安装了python)
http://www.cnblogs.com/cacique/archive/2012/09/29/2707976.html
二、数据库的配置
Django框架的唯一需求是结合Python,所以数据库在Django的Web开发中并不是至关重要的,但是在实际的Web设计中,大部分数据仍然保存在数据库中。Django支持多种数据库,包括MySQL、PostgreSQL和SQLite等。Django有着一个设计良好的ORM(对象-关系映射),可以有效的屏蔽底层数据库的不同。
对于每个Django应用,其目录中都有一个setting.py文件,用来实现对数据库的配置。
Ubuntu安装使用数据库系统Sqlite3
- 安装数据库系统Sqlite3:
apt-get install sqlite sqlite3
运行 python manage.py syncdb 报错:
django.core.exceptions.ImproperlyConfigured: 'sqlite3' isn't an available database backend.
Try using django.db.backends.sqlite3 instead.
Error was: No module named base
仔细看报错,怀疑可能是 Django1.5 里面settings简写sqlite3不行,好像得写全django.db.backends.sqlite3。于是改了一下settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': './djangopro.db3', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
红色字段为修改后的数据库引擎名称
绿色字段为创建的数据库路径和名字。
回到项目根目录下执行
#./manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'czh'): czh
Email address: 357258996@qq.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
可生成绿色字段的数据库文件。
安装可视化数据库工具:# sudo apt-get install sqlitebrowser
生成Django应用#./manage.py startapp Users
创建数据模型czh@ubuntu:~/workdir/PythonWeb/Django_Demo/Users$ cat models.py
#-*- coding: UTF-8 -*-
from django.db import models
# Create your models here.
class Users(models.Model):
username = models.CharField('用户名',max_length=20)
password = models.CharField('密码',max_length=20)
realname = models.CharField('真实姓名',max_length=255)
sex = models.CharField('性别',max_length=10)
email = models.EmailField('电子邮箱',blank=True)
def __str__ (self):
return '%s' % (self.name)
注意:这里如果没有加 #-*- coding: UTF-8 -*-会打印:SyntaxError: Non-ASCII character '\xe7' in file /home/czh/workdir/PythonWeb/Django_Demo/Users/models.py on line 5, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (models.py, line 5)
然后在settings.py文件中加入此应用INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'Users', #从项目根目录下开始的路径,一定要正确,否则会报错ImportError: No module named Users,我的Users目录就在项目根目录下,所以前面没加其他路径。根据具体情况而定
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
修改完毕后,回到manage.py相应的目录下执行:# ./manage.py syncdb
Creating tables ...
Creating table Users_users
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
生成一个名为Users_users的表使用前面安装的图形工具可以查看数据表。
显示出相应字段。
ok