在通用视图中实现表单的 Django 问题

Django problem with implementing a form within generic view

我正在开发允许用户共享图片(类似 Instagram)的应用程序,但我一直坚持在 DetailView 中实现向图片添加评论的功能。 即:在我的评论表单中输入数据并单击提交按钮后,我收到 404 错误

以下是我的观点:

@method_decorator(login_required, name='dispatch')
class ItemInterest(SingleObjectMixin, FormView):
    template_name = 'content/item_detail.html'
    form_class = CommentForm
    model = Item

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super().post(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('item-detail', kwargs={'pk': self.object.pk})


@method_decorator(login_required, name='dispatch')
class ItemDisplayView(DetailView):
    model = Item

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = CommentForm()
        return context

@method_decorator(login_required, name='dispatch')
class ItemDetailView(View):

    def get(self, request, *args, **kwargs):
        view = ItemDisplayView.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = ItemInterest.as_view()
        return view(request, *args, **kwargs)

型号:

  class Item(models.Model):
    description = models.CharField(max_length=255)
    photo = models.ImageField(upload_to='photos')
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)


  class Comment(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='comments')
    author = models.CharField(max_length=255)
    body = models.TextField()
    create_date = models.DateTimeField(default=timezone.now)
    active = models.BooleanField(default=True)

评论表:

class CommentForm(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ('author', 'body', 'item',)

和url:

path('item/<int:pk>/', ItemDetailView.as_view(), name='item-detail'),

item_detail.html:

  <div class="container mt-3 mx-auto jumbotron d-flex flex-column align-items-center" >
        <div class="user-info d-flex justify-content-center m-2">
            <img src="{{ object.author.profile.image.url }}" alt="">
            <p class="ml-5"><a href="{% url 'profile' object.author %}">>{{ object.author }}  </a>  <small>{{ object.date_posted }}</small></p>
        </div>
        <img src="{{ object.photo.url }}" alt="">
        <p>{{ object.description }}</p>

        {% for comment in object.comments.all %}
            <div class="container">
                {{ comment.create_date }}
                <strong>{{ comment.author }}</strong>
                <p>{{ comment.bod y}}</p>
            </div>
        {% empty %}
            <p>no comments yet</p>
        {% endfor %}
        <form action="POST">
            {% csrf_token %}
            {{ form.as_p }}
             <button type="submit">add comment</button>
        </form>
        {% if object.author == user %}
            <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'item-update' object.id %}">update</a>
            <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'item-delete' object.id %}">delete</a>
        {% endif %}

    </div>

您填写的表格有误

<form action="POST">

必须是

<form method="POST">