[Fixed]-Django โ€“ Passing template context to form

1๐Ÿ‘

โœ…

You will need to create a url with named group for post.id to capture the id of post for which comment is being created.

url(r'app/post/(?P<post_id>\d+)/comment/create', show_question_page, name='create-comment')

You can pass post.id in action url of the form.

action={% url 'create-comment' post.id %}

And in view, you can get the passed post_id from request object and create a comment related to post.

    comment = comment_form.save(commit=False)
    post_id = request.POST.get('post_id')
    comment.post = get_object_or_404(QuestionPost, id=post_id)
    comment.user_profile = UserProfile.objects.filter(user=request.user)

Leave a comment