我是django的新手,我创建了一个基于函数的视图,为一个问题创建了选择。
def create_choice(request, question_id):
question = get_object_or_404(Question, pk=question_id)
if request.method == 'POST':
form = ChoiceForm(request.POST)
if form.is_valid():
choice = form.save(commit=False)
choice.question = question
choice.save()
return HttpResponseRedirect(urlresolvers.reverse('polls:detail', kwargs={'question_id':question_id}))
else:
form = ChoiceForm()
context = {'form':form, 'question':question}
return render(request, 'polls/add_choice.html', context)
然后我尝试将其转换为基于类的视图......
class ChoiceView(generic.DetailView):
template_name = 'polls/add_choice.html'
def get(self, request, *args, **kwargs):
form = ChoiceForm()
question_id = self.kwargs['pk']
question = get_object_or_404(Question, pk=question_id)
context = {'form':form, 'question':question}
return render(request, 'polls/add_choice.html', context)
def post(self, request, *args, **kwargs):
form = QuestionForm(request.POST)
if form.is_valid():
choice = form.save(commit=False)
choice.question = question
choice.save()
return HttpResponseRedirect(urlresolvers.reverse('polls:detail', kwargs={'question_id':question_id}))
但是当我在runserver中添加一个选项时,我得到“视图polls.views.ChoiceView没有返回一个HttpResponse对象。它返回了None。”我做错了什么?为什么我没有获得GET数据?