0

我正在尝试在我的 Mac 上设置 Python/Django/Mysql,但是当我在终端中运行此命令时,我不断收到以下错误

Python manage.py runserver

我得到的错误是

Marks-MacBook-Air:FirstBlog mmillar$ python manage.py runserver
Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of      <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1017c2b10>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 92, in inner_run
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 280, in validate
 num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 166, in get_app_errors
self._populate()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 72, in _populate
self.load_app(app_name, True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 96, in load_app
models = import_module('.models', app_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/mmillar/PycharmProjects/FirstBlog/blog/models.py", line 5, in <module>
class post(models.Model):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 145, in __new__
new_class.add_to_class(obj_name, obj)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 265, in add_to_class
value.contribute_to_class(cls, name)
TypeError: Error when calling the metaclass bases
unbound method contribute_to_class() must be called with TextField instance as first argument (got ModelBase instance instead)

模型.py

from django.db import models 
# Create your models here. 

class post(models.Model): 
    author = models.CharField(max_length=30) 
    title = models.CharField(max_length=100) 
    bodytext = models.TextField 
    timestamp = models.DateTimeField 
4

2 回答 2

0

我认为你做了这样的事情:

错误的:

class Foo(models.Model):
    bar = models.TextField

正确的:

class Foo(models.Model):
    bar = models.TextField()

因此,您尝试使用类而不是类实例进行验证。

于 2013-10-03T14:09:30.027 回答
0

您的 models.py 必须如下:

from django.db import models 
# Create your models here. 

class post(models.Model): 
    author = models.CharField(max_length=30) 
    title = models.CharField(max_length=100) 
    bodytext = models.TextField()
    timestamp = models.DateTimeField() 

原因unbound method contribute_to_class() TypeError- 表示

“模型字段未正确声明,字段类型名称后的括号丢失”

您缺少 2 个字段的括号:

bodytext = models.TextField 
timestamp = models.DateTimeField 
于 2016-04-18T11:12:11.590 回答