PyDjango
PyDjango

Reputation: 11

The comment form is displayed incorrectly



I am making comments on django on this video

https://www.youtube.com/watch?v=bmFkND-scpY

when the author in the middle of the video showed what he had done, he showed this (this is how it should be) enter image description here

but for some reason it turned out like this enter image description here

class Comment in models.py

    class Comment(models.Model):
        post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
        name = models.CharField(max_length=255, default="Some String")
        body = models.TextField(max_length=255, null=True, blank=True)
        date_added = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return '%s - %s' % (self.post.title, self.name)

forms.py

from django.forms import ModelForm

from .models import Comment


class CommentForm(ModelForm):
    class Meta:
        model = Comment
        fields = ['name', 'body']

def detail in views.py

def detail(request, slug):
    post = Post.objects.get(slug=slug)
    form = CommentForm()
    context = {
        'post': post,
        'form': form
    }

    return render(request, 'myblog/templates/post_detail.html', context)
 

post_detail.py

{% extends 'base.html' %}

{% block content %}
    <div class="post-entry">
        <h2>{{ post.title }}</h2>
        <p>{{ post.body }}</p>
    </div>

    <p><a href="{% url 'post_edit' post.pk %}">+ Edit Blog Post</a></p>
    <p><a href="{% url 'post_delete' post.pk %}">+ Delete Blog Post</a></p>
    <img src="{{ post.header_image.url|default_if_none:'#' }}">


    {{ post.body|urlize }}


    {% for comm in post.commentpost_set.all%}
        {{ comm.user }} <br>
        {{ comm.text }} <br><br>
    {% endfor %}

    <article class="content">

        <br><hr>
        <h2>Add a comment</h2>

        <form method="post" action=".">
            {% csrf_token %}

            {{ form.as_table }}

            <input type="submit" value="Submit">
        </form>
    </article>

{% endblock content %}

if necessary, tell me, I'll throw off the files completely, I just didn't want to clog the question with a lot of code Thanks a lot for any help!

Upvotes: 1

Views: 35

Answers (0)

Related Questions