Reputation: 41
I want to link a comment model to post when multiple posts are displayed in a page i want to access the post id of the post i am commenting on but i do not know how please tell me how to link the comment model I am a begginner here so i apologize for any errors Please comment if any more of code is needed
template :
{% for Img in imgs %}
<div class="container">
<div class="card">
<div class="card-image waves-effect waves-block waves-light">
<img class="activator" src="{{ Img.Post_Img.url }}" alt="image">
</div>
<div class="card-content">
<img src="{{ Img.op.userprofile.Profile_pic.url }}" class="profiles" alt="{{ Img.op.userprofile.Nick_Name }}">
<span class="OP">{{ Img.op.userprofile.Nick_Name }}</span>
<span class="card-title activator white-text text">{{ Img.Title }}<i class="material-icons right">more_vert</i></span>
<p><a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">Vote</a></p>
</div>
<div class="card-reveal darken-4 black">
<span class="card-title white-text text">{{ Img.Title }}<i class="material-icons right">close</i></span>
<form method="post">
{% csrf_token %}
{{ comment }}
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
{% for comment in comments %}
{{ comment.op.userprofile.Nick_Name }}{{ comment.comment }}
{% endfor %}
</div>
</div>
</div>
{% endfor %}
models :
class MemeImg(models.Model):
Title = models.CharField(max_length=500)
date_created = models.DateTimeField(auto_now_add=True)
op = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
Post_Img = CloudinaryField('Post')
def __str__(self):
return self.Title
class Comments(models.Model):
post = models.ForeignKey(MemeImg, on_delete=models.CASCADE)
op = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
comment = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.post.Title
views ( does not work throws error not null contraint):
def user_home(request):
func = data(request)
url = func[0]
name = func[1]
comment = CommentSection(request.POST)
if request.method == 'POST':
comment = CommentSection(request.POST)
if comment.is_valid():
comment.save(commit=False)
comment.op = request.user
comment.save()
comments = Comments.objects.all()
imgs = MemeImg.objects.all()
ctx = {
'imgs': imgs,
'url': url,
'name': name,
'comment': comment,
'comments': comments,
}
return render(request, 'User_Home.html', ctx)
Upvotes: 0
Views: 201
Reputation: 743
You need to specify null=True
and blank=True
for the ForeignKey in the Comments
model, or you must set a default value in when you makemigrations
.
By default, None
values aren't allowed for fields, without you explicitly allowing them, because the post
property of the Comments
model is set to null=False
by default.
When you create a comment and something else you've written results in comment.post = None
, then Django tries to find a default value. But you haven't specified a default value either, so Django raises the error because it can't find anything to fill in a field that's supposed to have something in it.
So the post field of the Comments
model is supposed to look like:
post = models.ForeignKey(MemeImg, on_delete=models.CASCADE, null=True, blank=True)
Upvotes: 1