我是刚刚学习Django,所以在学习中遇到了一些问题,希望我的分享对大家有一点用处。
1.安装python2.5.x.
2.安装MySQL.
3.安装MySQL-python-1.2.2.win32-py2.5.exe。
4.想必大家在生成mysite的时候没有什么问题吧,mysite下面的settings.py中mysql数据库的配置如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MySQL',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '',
'PORT': '3306', }
}
配置完后,测试一下数据库吧。
记得把MySQL数据库打开。
转到创建的mysite 项目目录,运行 python manage.py shell 命令
>>> from django.db import connection
>>> cursor = connection.cursor()
转到mysite 项目目录,执行下面的命令来创建一个新app叫做books:
python manage.py startapp books
from django.db import models
class Publisher(models.Model):
name = models.CharField(maxlength=30)
address = models.CharField(maxlength=50)
city = models.CharField(maxlength=60)
state_province = models.CharField(maxlength=30)
country = models.CharField(maxlength=50)
website = models.URLField()
class Author(models.Model):
salutation = models.CharField(maxlength=10)
first_name = models.CharField(maxlength=30)
last_name = models.CharField(maxlength=40)
email = models.EmailField()
headshot = models.ImageField(upload_to='/tmp')
class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
需要把变量maxlength改为max_length要不然会出现错误,如下:
File "D:\installFiles\python252\Lib\site-packages\django\utils\importlib.py",
line 35, in import_module
__import__(name)
File "D:\installFiles\python252\Lib\site-packages\django\bin\mysite\..\mysite\
books\models.py", line 2, in <module>
class Publisher(models.Model):
File "D:\installFiles\python252\Lib\site-packages\django\bin\mysite\..\mysite\
books\models.py", line 3, in Publisher
name = models.CharField(maxlength=30)
File "D:\installFiles\python252\Lib\site-packages\django\db\models\fields\__in
it__.py", line 545, in __init__
super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'maxlength'
def __init__(self, *args, **kwargs):
super(CharField, self).__init__(*args, **kwargs)
self.validators.append(validators.MaxLengthValidator(self.max_length))
在fields\__init__.py的line545定义的变量为max_length,而不是maxlength,所以需要把maxlength改为max_length就OK了,现在运行python manage.py validate就没有问题了。
1.安装python2.5.x.
2.安装MySQL.
3.安装MySQL-python-1.2.2.win32-py2.5.exe。
4.想必大家在生成mysite的时候没有什么问题吧,mysite下面的settings.py中mysql数据库的配置如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MySQL',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '',
'PORT': '3306', }
}
配置完后,测试一下数据库吧。
记得把MySQL数据库打开。
转到创建的mysite 项目目录,运行 python manage.py shell 命令
>>> from django.db import connection
>>> cursor = connection.cursor()
转到mysite 项目目录,执行下面的命令来创建一个新app叫做books:
python manage.py startapp books
from django.db import models
class Publisher(models.Model):
name = models.CharField(maxlength=30)
address = models.CharField(maxlength=50)
city = models.CharField(maxlength=60)
state_province = models.CharField(maxlength=30)
country = models.CharField(maxlength=50)
website = models.URLField()
class Author(models.Model):
salutation = models.CharField(maxlength=10)
first_name = models.CharField(maxlength=30)
last_name = models.CharField(maxlength=40)
email = models.EmailField()
headshot = models.ImageField(upload_to='/tmp')
class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
需要把变量maxlength改为max_length要不然会出现错误,如下:
File "D:\installFiles\python252\Lib\site-packages\django\utils\importlib.py",
line 35, in import_module
__import__(name)
File "D:\installFiles\python252\Lib\site-packages\django\bin\mysite\..\mysite\
books\models.py", line 2, in <module>
class Publisher(models.Model):
File "D:\installFiles\python252\Lib\site-packages\django\bin\mysite\..\mysite\
books\models.py", line 3, in Publisher
name = models.CharField(maxlength=30)
File "D:\installFiles\python252\Lib\site-packages\django\db\models\fields\__in
it__.py", line 545, in __init__
super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'maxlength'
def __init__(self, *args, **kwargs):
super(CharField, self).__init__(*args, **kwargs)
self.validators.append(validators.MaxLengthValidator(self.max_length))
在fields\__init__.py的line545定义的变量为max_length,而不是maxlength,所以需要把maxlength改为max_length就OK了,现在运行python manage.py validate就没有问题了。