注释表单显示不正确

2024-04-19 14:58:49 发布

您现在位置:Python中文网/ 问答频道 /正文



我在这段视频中对django发表评论

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

当作者在视频的中间显示他做了什么时,他展示了这个(这应该是这样)。 enter image description here

但出于某种原因,结果是这样的 enter image description here

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']

视图.py中的def详细信息

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 %}

如果有必要的话,告诉我,我会完全扔掉这些文件,我只是不想用太多的代码来阻塞这个问题 非常感谢你的帮助