Reputation: 318
Hi have this structure to handle users posts and comments
my post model is
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
profile = models.ForeignKey('users.Profiles', on_delete=models.CASCADE, null=True, default=1, blank=True)
title = models.CharField(max_length=100, blank=True, null=True)
desc = models.CharField(max_length=1000, blank=True, null=True)
photo = models.ImageField(upload_to='posts/photos')
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __str__(self):
return '{} by {}'.format(self.title ,self.user)
and my comment model
from django.db import models
from posts.models import Post as Post
from django.contrib.auth.models import User
class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE, null=True, default=1, blank=True)
name = models.CharField(max_length=20,blank=True, null=True)
email = models.EmailField(max_length=20,blank=True, null=True)
body = models.TextField(max_length=200,blank=True, null=True)
created = models.DateTimeField(auto_now_add=True,blank=True, null=True)
updated = models.DateTimeField(auto_now=True,blank=True, null=True)
active = models.BooleanField(default=True,blank=True, null=True)
class Meta:
ordering = ('created',)
def __str__(self):
return 'Comment by {} on {}'.format(self.name, self.post)
im able to create post on admin
from posts.models import Post as Post
from django.contrib import admin
from comments.models import Comment
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'post', 'created', 'active')
list_filter = ('active', 'created', 'updated')
search_fields = ('name', 'email', 'body')
can create my post and persist on db
now i have this function to persist the comment on front
from django.shortcuts import render
from .models import Post, Comment
from .forms import EmailPostForm, CommentForm
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
# List of active comments for this post
comments = post.comments.filter(active=True)
new_comment = None
if request.method == 'POST':
# A comment was posted
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request,
'blog/post/detail.html',
{'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
now on my feed.html i have the list of post that i iterate with a for and with a inner for i iterate the comments
feed.html
<div class="row"style= "align:center">
{% for post in posts %}
<div class="col-sm-12 col-md-8 offset-md-4 mt-5 p-0 post-container,width:50%;">
<div class="card" style="width: 32rem;width:50%;">
<div class="card-body">
<div class="media pt-3 pl-3 pb-1">
<a href="{% url "users:detail" post.user.username%}">
<img class="mr-3 rounded-circle" height="35" src="{{ post.profile.picture.url }}" alt="{{ post.user.username }}">
</a>
<h3 class="card-title">{{ post.title }}</h3>
</div>
<p class="card-text">{{ post.desc }}</p>
</div>
</div>
<img style="width: 50%; heigth:60%" src="{{ post.photo.url }}" alt="{{ post.title }}">
<div class="media-body">
<b> <p style="margin-top: 5px;">@{{ post.user.username }} - <small>{{ post.created }}</small>
<a href="" style="color: #000; font-size: 20px;">
<i class="far fa-heart"></i>
</a>
<br>
</p></b>
</div>
</div>
{% for comment in comments.post %}
<div class="comment">
<p class="info">
Comment {{ forloop.counter }} by {{ comment.name }}
{{ comment.created }}
</p>
{{ comment.body|linebreaks }}
</div>
{% empty %}
<p>There are no comments yet.</p>
{% endfor %}
{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
<h2>Add a new comment</h2>
<form action="." method="post">
{{ comment_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Add comment"></p>
</form>
{% endif %}
{% endfor %}
</div>
</div>
but doing this i get this on the from for a post with comments
im missing some url (to call post_detail )and action on the comment for ? why im unable to see the list of comments for a post ?
Upvotes: 1
Views: 287
Reputation: 7330
Here in your Post
model there is no field for slug
. So instead of slug pass the post pk
to the detail view like this.
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
# now you can filter the comments here
comments = post.comment_set.filter(active=True)
return render(request,'blog/post/detail.html',{'post': post,'comments': comments}
See the docs for more info
Upvotes: 2