Home > Net >  Getting id of instance in form and save in it
Getting id of instance in form and save in it

Time:10-23

I am building a Blog App and I am working on a feature in which A user can report comment so I created another model for storing reports so i am saving which comment is reported But I placed report form in detail view so report form will be below the comment in post detail page, In which I am not getting the comment id when reporting.

models.py

class Blog(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=1000)

class Comment(models.Model):
    commented_by = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.CharField(max_length=1000)

class ReportComment(models.Model):
    reported_by = models.ForeignKey(User, on_delete=models.CASCADE)
    reported_comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
    text = models.CharField(max_length=1000)

views.py

def blog_detail_page(request, blog_id):
    post = get_object_or_404(Blog, pk=blog_id)

    if request.method == 'POST':
        reportform = CommentReportForm(data=request.POST)
        if FlagCommentForm.is_valid():
            form = reportform.save(commit=False)
            # Saving in this line
            flagForm.reported_comment = reportform.id
            form.reported_by = request.user
            form.save()
            return redirect('home')

    else:
        reportform = CommentReportForm()




    context = {'reportform':reportform, 'post':post}
    return render(request, 'blog_detail_page.html', context)

blog_detail_page.html


{{post.title}}


{% for comment in post.comment_set.all %}

{{comment.body}}


<div class="container">
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <table>
        {{ reportform }}
        </table>
        <button type="submit">Save</button>
    </form>
</div>

{% endfor %}

What have I tried :-

  • I have also tried by using loop like :-
comments = post.comment_set.all()

    for com in comments:
        if request.method == 'POST':
            ......
            if reportform.is_valid():
            ....
            ......
            ......
            form.reported_by = com

But it always saved the first comment id.

  • Then I tried by request.POST method, Like :-
comment_ID = request.POST['comment_id']

But is showed MultiValueDictKeyError error.

I have tried many times But the id of comment is not saving with report instance.

Any help would be much Appreciated. Thank you

CodePudding user response:

You will need to add the primary key of the comment to the form, or to the URL where you submit the form to. For example as a hidden form element:

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="hidden" name="comment_id" value="{{ comment.pk }}">
    <table>
    {{ reportform }}
    </table>
    <button type="submit">Save</button>
</form>

An alternative is to make a URL where you report a comment to with:

urlpatterns = [
    path('comment/<int:comment_id>/report', some_view, name='report-comment')
]

then you can submit the form to that view with:

<form method="post" action="{% url 'report-comment' comment_id=comment.pk %}" enctype="multipart/form-data">
    {% csrf_token %}
    <table>
    {{ reportform }}
    </table>
    <button type="submit">Save</button>
</form>
  • Related