【问题标题】:pub_date is invalid in Django tutorial errorpub_date 在 Django 教程错误中无效
【发布时间】:2013-01-21 07:58:45
【问题描述】:
C:\mysite>python manage.py shell
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from polls.models import Poll,Choice
>>> Poll.objects.all()
[]
>>> import django
>>> from django.utils import timezone
>>> p= Poll(question="what's new?",pub_date= timezone.now())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 367, in __init__
    raise TypeError("'%s'is an invalid keyword argument for
                          this function"%kwargs.keys()   [0])
TypeError: 'pub_date' is an invalid keyword argument for this function

【问题讨论】:

  • 你能展示一下你的 Poll 类的代码吗?是否有字段pub_date

标签: python django web-applications web-application-design


【解决方案1】:

检查你的 models.py 可能你打错了 pub_date 日期时间字段

【讨论】:

  • 是的,我明白了...但是我已经设置了,但是这里出现了新的错误
【解决方案2】:

有点晚了,但我也遇到了这个问题,我找到了答案。将你的 models.py 文件更改为:

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2018-11-13
    相关资源
    最近更新 更多