Post Details

views.py

def post_view(request,post_id) :
    post = get_object_or_404(Post,title=post_id)  
    comments= Comment.objects(post=post)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post=post
            comment.save()
            return redirect('post_view',post.title)
    
    else:
        form = CommentForm()        
    return render(request,'blog/details.html',{'post':post,'comments':comments,'form':form})  

html

    {{post.title}} <br>
    {{post.content}} <br>
    {{post.categories}} <br>
    {% for tag in post.tags %}
    <button>{{tag.name}} </button> 
    {% endfor %}
    <a href="{% url 'edit_post' post.title %}"> Edit </a> <a href="{% url 'delete_post' post.title %}"> Delete </a>

    <h2> All Comment </h2>
    {% for comment in comments %}
    {{comment.name}} <br>
    {{comment.content}}
    {% endfor %}
    <h2> Post Your Comments </h2>
    <form  method="post">
        {% csrf_token %}
        {{ form.as_p }}

        <button type="submit">Save</button>
    </form>

Last updated