英文:
Form doesn't appear when mixing DetailView and FormView in single view
问题
以下是您要翻译的内容:
我正在遵循这个链接 https://docs.djangoproject.com/en/3.0/topics/class-based-views/mixins/#an-alternative-better-solution ,以便每个图书详细信息视图下都可以发布评论。
class BookDetailView(DetailView):
model = Book
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = BookReviewForm()
return context
class BookComment(SingleObjectMixin, FormView):
template_name = 'book/book_detail.html'
form_class = BookReviewForm
model = Book
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def get_success_url(self):
return reverse('book_detail', args=[str(self.object.id)])
class BookDetail(View):
def get(self, request, *args, **kwargs):
view = BookDetailView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = BookComment.as_view()
return view(request, *args, **kwargs)
所使用的表单是:
class BookReviewForm(forms.Form):
class Meta:
model = Review
fields = ('review', 'score',)
评论模型:
class Review(models.Model):
book = models.ForeignKey(
Book,
on_delete=models.CASCADE,
related_name='reviews',
)
review = models.CharField(max_length=5000)
score = models.PositiveSmallIntegerField(null=True, blank=True,
validators=[MinValueValidator(0), MaxValueValidator(10)])
author = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
class Meta:
unique_together = ('book', 'author')
def __str__(self):
return self.review
然而,当我将表单添加到模板中时:
<div>
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Send</button>
</form>
</div>
它不会显示,只有提交按钮。如果我单击按钮,我会被正确重定向。如何使表单显示出来?
英文:
I am following this https://docs.djangoproject.com/en/3.0/topics/class-based-views/mixins/#an-alternative-better-solution so that for ever Book detail view users can post reviews below.
class BookDetailView(DetailView):
model = Book
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = BookReviewForm()
return context
class BookComment(SingleObjectMixin, FormView):
template_name = 'book/book_detail.html'
form_class = BookReviewForm
model = Book
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def get_success_url(self):
return reverse('book_detail', args=[str(self.object.id)])
class BookDetail(View):
def get(self, request, *args, **kwargs):
view = BookDetailView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = BookComment.as_view()
return view(request, *args, **kwargs)
The form used is:
class BookReviewForm(forms.Form):
class Meta:
model = Review
fields = ('review', 'score',)
The Review model:
class Review(models.Model):
book = models.ForeignKey(
Book,
on_delete=models.CASCADE,
related_name='reviews',
)
review = models.CharField(max_length=5000)
score = models.PositiveSmallIntegerField(null=True, blank=True,
validators=[MinValueValidator(0), MaxValueValidator(10)])
author = models.ForeignKey(
get_user_model(),
on_delete=models.CASCADE,
)
class Meta:
unique_together = ('book', 'author')
def __str__(self):
return self.review
Yet when I add the form to the template:
<div>
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Send</button>
</form>
</div>
it doesn't appear, only the submit button. If I click the button, I get redirected correctly. How can I make the form appear?
答案1
得分: 0
class BookReviewForm(forms.ModelForm):
class Meta:
model = Review
fields = ('review', 'score',)
Please change the form to model form
英文:
class BookReviewForm(forms.ModelForm):
class Meta:
model = Review
fields = ('review', 'score',)
Please change the form to model form
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论