我试过django投票应用程序,但得到了这个,我的模型包含了外键,但我不明白问题在哪里
from django.db import models
class Question(models.Model):
question = models.CharField(max_length = 200)
pub_date = models.DateTimeField('published date')
def __str__(self):
return self.question
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
vote = models.IntegerField(default=0)
def __str__(self):
return self.choice_text下面是视图:
def vote(request , primary_key):
question = get_object_or_404(models.Question, id=primary_key)
try:
selected_choice = question.choice_set.get(id=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request,'polls/detail.html',{
'question' : question ,
'error_message' : "u didn't select a choice."
})
else:
selected_choice.vote += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args = (question.id)))urls:
urlpatterns = [
re_path(r'^$', views.index , name = 'index'),
re_path(r'^(?P<primary_key>[0-9]+)/$',views.detail, name = 'detail'),
re_path(r'^(?P<primary_key>[0-9]+)/results/$' , views.results , name = 'results'),
re_path(r'^(?P<primary_key>[0-9]+)/vote/$' , views.vote , name = 'vote'),]
发布于 2020-05-16 01:23:44
您是否进行了迁移,并在定义了Choice类之后进行了迁移?
https://stackoverflow.com/questions/61773560
复制相似问题