简体   繁体   English

Django 模板什么都不显示

[英]Django template displaying nothing

I have a TextField(text area) form on my page where users can submit comments and have them displayed.我的页面上有一个 TextField(文本区域)表单,用户可以在其中提交评论并显示它们。

I've left several comments and none of them is showing up.我留下了几条评论,但没有一条出现。 There's just a bunch of empty HTML tags for all the comments i left, cant figure what the issue is我留下的所有评论只有一堆空的 HTML 标签,不知道是什么问题

models.py:模型.py:

class Comments(models.Model):
    comment = models.TextField(max_length=250)
    user_commented = models.CharField(max_length=64)
    list_title = models.CharField(max_length=64)
    list_author = models.CharField(max_length=64)
    date_time = models.DateTimeField(default=timezone.now, blank=True)

    def __str__(self):
        return f"{self.user_commented}, {self.date_time}, {self.comment}"

forms.py forms.py

class CommentForm(ModelForm):
    class Meta:
        model = Comments
        fields = ['comment']

views.py视图.py

commentform = CommentForm()

comment = CommentForm(request.POST)

if "comment" in request.POST:
            if comment.is_valid:
                comment_data = Comments.objects.create(list_title=title, user_commented=username, list_author=author, comment=comment)
                comment_data.save()
                comment_data = list(Comments.objects.all().filter(list_title=title))
                return render(request, "auctions/listing.html", {
                        "form": form,
                        "listing": listing_object,
                        "checkbox": checkbox,
                        "commentform": commentform,
                        "max_bid": max_bid,
                        "comments": comment_data
                    })

template模板

<form action="{% url 'listing' listing.title %}" method="POST">
        {% csrf_token %}
        {{ commentform }}
        <input type="submit" value="Comment" name="comment">
    </form>

    <div class="comment">
        <h5>Comments</h5>
        {% for comment in comments %}
            <p>{{ comments.user_commented }}</p><span>{{ comments.date_time }}</span>
            <p>{{ comments.comment }}</p>
            <br>
        {% endfor %}
    </div>

GET request: I grabbed all the comments from the database into a variable before passing it to the GET request like so GET 请求:我将数据库中的所有评论都抓取到一个变量中,然后再像这样将其传递给 GET 请求

comment_data = list(Comments.objects.all().filter(list_title=title))


return render(request, "auctions/listing.html", {
        "form": form,
        "listing": listing_object,
        "checkbox": checkbox,
        "commentform": commentform,
        "max_bid": max_bid,
        "users": author,
        "comments": comment_data
    })

You did wrong here:你在这里做错了:

      {% for comment in comments %}
            <p>{{ comment.user_commented }}</p><span>{{ comment.date_time }}</span>
            <p>{{ comment.comment }}</p> #You had added comments here instead of comment
            <br>
        {% endfor %}

According to for loop, you had added comments instead of comment .根据 for 循环,您添加了comments而不是comment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM