Hi everyone,
I am learning Django and trying to develop a small website. This Website has many users and every user can create posts as well as can comment other posts too. Because of that I created three models: My_User, Post, Comment. When when try to add a comment on a Post, I got the error : “OperationalError at /register/posts/Table
table Register_comment has no column named author_id”. Even in Shell I am getting the same error!
However; the user can add new Post.
I tried a lot to solve but it was not possible. I hope someone can help me. Thanks in Advance.
Down I added my models.py and views.py:
#models.py:
class My_User(models.Model):
# user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='my_user',default="")
username = models.CharField(max_length=50, blank=True)
nationality = models.CharField(max_length=50)
email = models.CharField(max_length=50, blank=True)
is_active = models.BooleanField(blank=True, default=False)
email_verified = models.BooleanField(blank=True, default=False)
image = models.ImageField(upload_to='images/', blank=True)
def save(self, *args, **kwargs):
return super().save(*args, **kwargs)
def __str__(self):
# return f"{self.username} ({self.nationality})"
return self.username
class Post(models.Model):
title = models.CharField(max_length=150)
content = models.TextField(validators=[MinLengthValidator(10)])
author = models.ForeignKey(My_User, on_delete=models.SET_NULL, null=True, related_name="posts")
image = models.ImageField(upload_to="posts", null=True)
def __str__(self):
return f"{self.title} {self.author}"
class Comment(models.Model):
author = models.ForeignKey(My_User,null=True, on_delete=models.CASCADE, related_name="commentsauthor")
comment_text = models.TextField(max_length=450, null=True,default="")
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")
def save(self, *args, **kwargs):
return super().save(*args, **kwargs)
#views.py
class PostDetailView(View):
def get(self, request, title):
post = get_object_or_404(Post, title=title)
comment_form = CommentForm()
return render(request, 'Register/post_detail.html', {
'post': post,
'comment_form': comment_form,
})
def post(self, request, title):
# current_user = My_User.objects.get(username=request.user)
post = get_object_or_404(Post, title=title)
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
print("############################################NOT Valid")
current_user = get_object_or_404(My_User, username=request.user)
comment = comment_form.save(commit=False)
hp = Comment(author=current_user,
comment_text=comment_form.cleaned_data['comment_text'],
post=post
)
hp.save()
return HttpResponseRedirect(reverse("post-detail-page", args=[title]))
context = {
"post": post,
"comment_form": comment_form,
}
return render(request, "Register/post_detail.html", context)
<!--post_detail.html -->
{% extends "base.html" %}
{% load static %}
{% block title %} Post Details {% endblock %}
{% block css_files %}
<link rel="stylesheet" href="{% static "Register/post_detail.css" %}">
{% endblock %}
{% block content %}
<section class="container">
<h1>{{ post.title }}</h1>
<p class="post-content">{{ post.content }}</p>
<img src="{{ post.image.url }}" alt="{{ post.author.username }}" class="img-fluid user-image">
<div class="author-info">
<p><a href="{% url 'user-detail-page' post.author.username %}">Author: {{ post.author }}</a> </p>
</div>
<div class="author-info">
<p>Logged in User: {{ user.username }}</p>
</div>
<!-- Add more details as needed -->
</section>
<section id="comment-form">
<h2>Your Comment</h2>
<form action="{% url "post-detail-page" post.title %}" method="POST">
{% csrf_token %}
{% for form_field in comment_form %}
<div class="my-form-control {% if form_field.errors %}invalid{% endif %}" >
{{ form_field.label_tag }}
{{ form_field }}
{{ form_field.errors }}
</div>
{% endfor %}
<button> Save Comment </button>
</form>
</section>
{% endblock %}