I'm building a Django powered blog-like app for practice and learning. I'm working on setting up a form for users to leave comments on posts. I have a Post model that takes in a User foreign key, and a Comment model that takes in a User foreign key and a Post foreign key (to identify the post the comment is tied to).
I know the way I have it setup is not yet functional, but I'm just trying to debug a CSRF issue I keep having. Here's my code:
models.py
class Comment(models.Model):
date_posted = models.DateTimeField(default=timezone.now)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
views.py
@login_required
def post_detail(request, post_id):
if request.method == 'POST':
print("posted")
return redirect('Home')
else:
comment_form = CommentForm()
context = {
'post': Post.objects.get(id=post_id),
'comments': Comment.objects.order_by('date_posted'),
'comment_form': comment_form
}
return render(request, 'feed/postdetail.html', context)
template, "postdetail.html"
<form method="POST" enctype="text/plain">
<div class=comment-line>
{% csrf_token %}
{{ comment_form }}
<button type="submit">Post</button>
</div>
</form>
And I DO have the following in my middleware
'django.middleware.csrf.CsrfViewMiddleware',
I keep getting an error stating, "CSRF verification failed. Request aborted" for the reason "CSRF token missing or incorrect." This only happens when I click the post button.
I'm only just learning Django, what am I doing wrong?