我是Django的新手,从通常的函数类型视图开始,但我被要求仅在基于类的视图上执行此操作。 所以有一个问题。我需要做一个观点,接下来的事情: - 显示选定的问题 - 显示该问题的所有答案 - 包含一个表单,允许为此问题添加新答案。
我的models.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
forms.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,其中存储了问题详细信息和答案表单。
Poblem解决了:
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中
答案 0 :(得分:0)
实际上,您的解决方案有两种替代方法可以避免覆盖post
方法:
一种是使用DetailView
并在上下文中添加表单。但是,在模板中,您需要为表单标记实际指定action
属性,使用相同的表单指向单独的FormView
。这个与您的解决方案类似,但有一个单独的FormView
,必要时可以单独使用。
另一个是使用FormView
,它还包含基于URL参数的Question
对象。您可以使用该对象显示其内容,填充表单的字段等。