表单在单个视图中混合使用DetailView和FormView时不显示。

huangapple go评论59阅读模式
英文:

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[&#39;form&#39;] = BookReviewForm()
        return context

class BookComment(SingleObjectMixin, FormView):
    template_name = &#39;book/book_detail.html&#39;
    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(&#39;book_detail&#39;, 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 = (&#39;review&#39;, &#39;score&#39;,)

The Review model:

class Review(models.Model):
    book = models.ForeignKey(
        Book,
        on_delete=models.CASCADE,
        related_name=&#39;reviews&#39;,
    )
    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 = (&#39;book&#39;, &#39;author&#39;)

    def __str__(self):
        return self.review

Yet when I add the form to the template:

&lt;div&gt;
            &lt;form method=&quot;POST&quot;&gt;{% csrf_token %}
        {{ form.as_p }}
        &lt;button type=&quot;submit&quot;&gt;Send&lt;/button&gt;
    &lt;/form&gt;
        &lt;/div&gt;

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 = (&#39;review&#39;, &#39;score&#39;,)

Please change the form to model form

huangapple
  • 本文由 发表于 2020年1月3日 17:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定