为什么我的模型中的post.post没有以html显示? (发布)

时间:2018-10-11 20:10:59

标签: python django

嗨,我正在尝试用html显示{{post.post}},以前可以,但是现在不行了。显示用户名和日期,并且该帖子正在运行..但未成功显示post.post。问题出在哪里?我在下面显示了输出,并突出显示了可以指示的内容。 TIA

html:

select id, max(complaint) as complaint
from t
group by id;

models.py:

select id, complaint
from t
where complaint = 1
union all
select id, complaint
from t
where not exists (select 1 from t t2 where t2.id = t.id and t2.complaint = 1);

views.py:

{% extends 'base.html' %}

{% block body%}
<div class="container">
    <h1>Home</h1>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
</form>
<h2>{{ text }}</h2>
{% for post in posts %}
    <h1>{{ post.post }}</h1>
    <p>Posted by <b>{{ post.user.get_full_name}}</b> on {{ post.date }}</p>
{% endfor %}
</div>
{% endblock %}

输出:

class Post(models.Model):
    post = models.CharField(max_length=500)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)

1 个答案:

答案 0 :(得分:0)

当表单无效时,您不会在发布请求的上下文数据中包含posts,只需将其添加到上下文中即可。

args = {'form': form, 'text': text}

args = {'form': form, 'text': text, 'posts': Post.objects.all()}