Search code examples
djangoformshttp-status-code-404django-generic-viewsdetailview

Django problem with implementing a form within generic view


I am working on app that allows users to share prictures (instagram like) and I got stuck on implementing functionality of adding comments to pictures in DetailView. Namely: after entering the data into my comment form and clicking the submit button I get 404 error

here are my views:

@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)

models:

  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)

comment form:

class CommentForm(forms.ModelForm):

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

and 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>

Solution

  • You have a mistake in the form

    <form action="POST">

    Must be

    <form method="POST">