我是 Django 的新手,从通常的函数类型视图开始,但我被要求仅在基于类的视图上执行此操作。所以有问题。我需要创建一个视图,它会执行以下操作: - 显示一个选定的问题 - 显示该问题的所有答案 - 包含一个表单,允许为此问题添加新答案。
我的模型.py:
from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
class Question(models.Model):
author = models.ForeignKey(User, related_name='questions')
title = models.CharField(max_length=255)
text = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
number_views = models.IntegerField(default=0)
number_answers = models.IntegerField(default=0)
def __unicode__(self):
return self.title
class Answer(models.Model):
author = models.ForeignKey(User, related_name='answers')
question = models.ForeignKey("Question", related_name="answers")
text = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.text
表格.py:
from django import forms
from apps.questions.models import Question, Answer
class QuestionForm(forms.ModelForm):
class Meta:
model = Question
fields = ['title', 'text']
class AnswerForm(forms.ModelForm):
class Meta:
model = Answer
fields = ['text']
来自views.py的代码,我从django docs中拿了一些类似的例子,但它不起作用......
class QuestionDetail(DetailView):
template_name = 'questions/detail.html'
model = Question
def get_context_data(self, **kwargs):
context = super(QuestionDetail, self).get_context_data(**kwargs)
context['form'] = AnswerForm()
return context
class AnswerView(FormView, SingleObjectMixin):
template_name = 'questions/detail.html'
form_class = AnswerForm
model = Answer
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return HttpResponseForbidden()
self.object = self.get_object()
return super(AnswerView, self).post(request, *args, **kwargs)
def get_success_url(self):
return reverse('detail', kwargs={'pk': self.object.pk})
class QuestionDisplay(View):
def get(self, request, *args, **kwargs):
view = QuestionDetail.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = AnswerView.as_view()
return view(request, *args, **kwargs)
和 urls.py
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.QuestionDisplay.as_view(), name='detail'),
)
此代码显示所选问题和相关答案的详细信息。但是当我在表单中输入一些新答案并单击提交时,它只会返回一个错误“找不到页面”,没有任何回溯或其他内容,并带有指向相同 URL 的链接,其中存储了问题详细信息和答案表单。
问题解决:
class QuestionDetail(DetailView, ModelFormMixin, ProcessFormView):
template_name = 'questions/detail.html'
model = Question
form_class = AnswerForm
def get_success_url(self):
return reverse('questions:detail', kwargs={'pk':self.get_object().id})
def get_context_data(self, **kwargs):
context = super(QuestionDetail, self).get_context_data(**kwargs)
context['form'] = self.form_class
return context
def post(self, request, *args, **kwargs):
self.object = Answer(author=request.user, question=self.get_object())
return super(QuestionDetail, self).post(request, *args, **kwargs)
和
url(r'^(?P<pk>\d+)/$', views.QuestionDetail.as_view(), name='detail'),
在 urls.py