Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36641
  • 博文数量: 3
  • 博客积分: 760
  • 博客等级: 军士长
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-24 19:11
文章分类
文章存档

2011年(1)

2008年(2)

我的朋友
最近访客

分类: Python/Ruby

2008-09-10 19:01:06

这几天在学习django,现在我把碰到的一些问题和解决方法,记录下来。环境是windows xp。
 
1.安装python2.5.
2.安装mysql5.0,和mysql-python,也就是MySQLdb这个包。注意在安装mysql时候,需要将所用的字符集设置成utf8,如果没有设置,则在建立数据的时候一定做设置成utf8,否则在利用django admin site的时候会出现错误。
3.安装django0.96或者django 1.0。这两个版本有一些不一样,其中一个问题是在models.py这个文件中,
django0.96用的方法是:
class Admin:
   pass
 
而django1.0用的方法是在models.py的最后结尾加上
from django.contrib import admin
admin.site.register(模型名称)
 
4,这几个文件都安装好以后,进去C:\Python25\Scripts,这个目录中有一个文件是django-admin.py,
现在需要利用这个文件生成工程,命令行下进入该目录,运行一个命令,命令是:
“python django-admin.py startproject 工程名”
我的工程名是Cosmetic,所以命令是“python django-admin.py startproject Cosmetic”
5,在Cosmetic目录下有三个文件,settings.py 和urls.py需要修改。
在settings.py中,修改:
设置模板路径:
import os.path
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),
)
 
设置数据库的一些参数:我的是这样的,
DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'cosmeticdb'            
DATABASE_USER = 'root'            
DATABASE_PASSWORD = 'root'        
DATABASE_HOST = ''            
DATABASE_PORT = ''       
 
5,建立一个app,利用命令"python manage.py startapp app_cosmetic   ",然后根据需求设计model,修改models.py文件,添加模型。
在这个过程中由于版本不同,会遇到不一样的情况:第一个是max_length 和maxlength要根据django版本不同而不同,另外一个很奇怪的问题,不知道是不是django的一个bug,当使用
/////////////////////
class Meta:
   db_table = "自定义表名"
////////////////////////
的时候,第一次运行"python manage.py syncdb正常,第二次再运行这个命令时候,会出现提示错误,说是table XXXX  已经在数据库中存在。
而djangobook上写得很明白,如果模型不改变的话,重复执行同步命令的时候是没问题的。
在djangobook 的第五章有如下文字:

The syncdb command is a simple “sync” of your models to your database. It looks at all of the models in each app in your INSTALLED_APPS setting, checks the database to see whether the appropriate tables exist yet, and creates the tables if they don’t yet exist. Note that syncdb does not sync changes in models or deletions of models; if you make a change to a model or delete a model, and you want to update the database, syncdb will not handle that. (More on this later.)

If you run python manage.py syncdb again, nothing happens, because you haven’t added any models to the books app or added any apps to INSTALLED_APPS. Ergo, it’s always safe to run python manage.py syncdb — it won’t clobber things.

这个问题困了我好久,刚开始不知道是指定了自定义表名的缘故,于是重装python,换版本重装mysql,重装mysql-python,问题依旧。

而原来在另外一台电脑上是没有出现这个问题,仔细想想,对比两个models.py的代码,也就多了下面这句话。

/////////////////////
class Meta:
   db_table = "自定义表名"
////////////////////////
 
后来将这个语句删除,问题解决。
 
 
 
 
 
 
 

 
阅读(555) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~