I tried a lot but could not find a solution I also try javascript but could not find a solution
def postPage(request, id):
post = Post.objects.get(pk=id)
showcomment = Comment.objects.filter(postby = id)
form = commentForm()
form = commentForm(request.POST)
form.instance.postby = post
if form.is_valid():
form.save()
form = commentForm()
redirect('postpage', id=post.sno)
context = {'post':post, 'form':form, 'showcomment':showcomment}
return render(request, 'blog/postpage.html', context)
form = commentForm() Only the field shows blank But the data is submitted when we refresh
HTML File
form action="." method="POST">
{{form}}
{% csrf_token %}
<input type="submit">
</form>
You can create a new form
immediately before you render the form. Note however that by creating a new form, the errors will dissapear. Furthermore for forms that are not that small, it is often wanted behavior to return a (partially) filled in form, such that the user can continue editing:
def postPage(request, id):
if request.method == 'POST':
form = commentForm(request.POST)
if form.is_valid():
form.instance.postby_id = id
form.save()
redirect('postpage', id=post.sno)
form = commentForm()
showcomment = Comment.objects.filter(postby=id)
context = {'post': post, 'form': form, 'showcomment': showcomment}
return render(request, 'blog/postpage.html', context)