简体   繁体   中英

Getting error when running the Django server on my Mac 10.8.5

I am trying to setup Python/Django/Mysql on my Mac but I keep getting the following error when I run this command in terminal

Python manage.py runserver

The error I get is

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)

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 

I think you did something like that:

wrong:

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

correct:

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

Thus you try to validate with a class instead of a class instance.

You models.py has to be as follows:

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() 

Cause : The unbound method contribute_to_class() TypeError - indicates

"A model field was not declared properly, the parentheses after the field types name were missing"

You have parenthesis missing for 2 fields:

bodytext = models.TextField 
timestamp = models.DateTimeField 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM