Code of Post model

class Post(models.Model):
 
    blog = models.ForeignKey(Blog, editable=False)
    title = models.CharField(max_length=100)
    slug = models.CharField(max_length=200, editable=False, unique=True)
    body = models.TextField()
    created_at = models.DateTimeField(auto_now_add = True)
    
    def get_absolute_url(self):
        return reverse('post',
                        kwargs={'username': self.blog.user.username,
                                'slug': self.slug})
    def save(self):
        if not self.id:
            self.slug = slugify(self.title)
        return super(Post, self).save()

    class Meta:
        ordering = ["-created_at"]

Code of Post controller (In Django called View)

class PostCreate(CreateView):
    model = Post
        
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(PostCreate, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        """
        This method is called when valid form data has been POSTed.
        The post has to be linked to the user's blog
        """
        
        form.instance.blog = self.request.user.blog
            
        return super(PostCreate, self).form_valid(form)


Code of view in Django (In Django called templates)

{% extends "base.html" %}

{% block content %}

    {% load blog_extras %} 

    {% show_post post %}

    <p> You can leave a comment</p>
    <form method="post" action="{% url 'post' post.blog.user post.slug %}" >
        {% csrf_token %}

        {{ form.as_p }}                    
        
        <input type="submit" value="Create a comment" />
    </form>
    <h3> Comments </h3>
    {% for comment in post.comment_set.all %}
        {% if comment.reg_user %}
            <h4> {{ comment.reg_user.username }} </h4>
            <h4> {{ comment.reg_user.userprofile.website }} </h4>
            <h4> {{ comment.reg_user.email }} </h4
        {% else %}
            <h4> {{ comment.user }} </h4>
            <h4> {{ comment.website }} </h4>
            <h4> {{ comment.email }} </h4>
        {% endif %}
        <p> {{ comment.body }} </p>
        {% if user.is_authenticated %}
            <form method="post" action="{% url 'comment_delete' post.blog.user post.slug comment.id %}" >
                {% csrf_token %}
                <input type="submit" value="Delete Comment" > 
            </form>
        {% endif %}
    {% endfor %}
{% endblock %}