“question_text”是此函数的无效关键字参数

2024-03-29 11:15:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图遵循Polls教程,包括来自项目的urls.py,以及Polls目录中的models.py。在

q = Question(question_text="some text", pub_date=timezone.now))

导致以下错误:

^{pr2}$

我的网站/网址.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

投票/模型.py

import datetime
from django.db import models
from django.utils import timezone

# Create your models here.

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


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

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

Tags: djangotextfrompyimportselfurldate
2条回答

如果您在djangoshell中执行此操作,首先需要导入正在使用的模型,在您的示例中,您需要导入model问题

让我们从头开始回忆一切。在

第1步:激活外壳

python manage.py shell

第2步:导入模型问题,同时导入时区

^{pr2}$

第3步:现在运行查询

q = Question(question_text="What's new?", pub_date=timezone.now())

如果你注意到了,根据你的问题,这里是你做错了什么。在

第4步:运行保存方法

q.save()

所有这些都是w.r.tPolls Tutorial在玩api。在

我认为在学习教程的过程中,您已经做了很多代码更改,而这些并没有反映在sql数据库中。在

所以,在做以上步骤之前,请遵循下面的步骤。在

python manage.py migrate
python manage.py makemigrations polls
python manage.py migrate

现在就这么做

^{pr2}$

相关问题 更多 >