【问题标题】:how to get new line in __repr__() in python flask?如何在 python 烧瓶中的 __repr__() 中获取新行?
【发布时间】:2019-09-27 07:58:52
【问题描述】:

我为我的博客页面创建了评论部分,但问题是它在一行中显示名称和评论,我希望这两个都在不同的行中。并且 \n 不工作。

class comments(db.Model):

    __tablename__ = 'comment'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.Text)
    comm = db.Column(db.Text)

    def __init__(self,name,comm):
        self.name = name
        self.comm = comm

    def __repr__(self):
        return f" Name: {self.name} Comment: {self.comm}"

功能

@app.route('/fat', methods=['GET','POST'])
def fat():

    form = AddForm()
    if form.validate_on_submit():
        name = form.name.data
        comm = form.comm.data
        newname = comments(name,comm)
        db.session.add(newname)
        db.session.commit()

        return redirect(url_for('fat'))

    new = comments.query.all()


    return render_template('fat.html',form=form, newnames=new)

下面附上fat.html的代码...................................... ..................................................... ..................................................... …………

</div>
{% block content %}
<div class="post comment">
    {% for co in newnames %}
    <li>{{co}}</li>
    {% endfor %}
</div>



<div class="myform">
    <h2>Leave a Comment</h2>
<form method="POST">

    {{ form.hidden_tag() }}
    <div class="myforms">
    <h1 class="myform1"> {{ form.name.label }}</h1>
    <h1 class="myform2"> {{ form.name() }} </h1>
    </div>
    <div class="myformss">
    <h1 class="myform3">    {{ form.comm.label }} </h1>
    <h1 class="myform4"> {{ form.comm()}}</h1>

    </div>
    <div class="myformsss">
        <h1 class="myform5"> {{ form.submit() }} </h1>
    </div>



</form>
</div>
{% endblock %}

【问题讨论】:

    标签: html flask flask-sqlalchemy flask-wtforms


    【解决方案1】:

    在后端做 HTML 内容布局不是那么灵活。显示风格应该在前端决定。我的意思是,你应该修改你的fat.html

    如果你需要我的帮助。修改你的问题,粘贴fat.html的内容。


    更新:

    <div class="post comment">
        {% for co in newnames %}
        <li>Name: {{ co.name }}</li>
        <li>Name: {{ co.comm }}</li>
        {% endfor %}
    </div>
    

    这是一个简单的例子。您可以像在 Python 中一样访问 Comment 对象。

    您可能需要更改 css 样式以缩进 cmets 块并使其与其他块对齐。

    【讨论】:

    • 嘿,我附上了 fat.html 文件。请调查一下
    猜你喜欢
    • 2021-08-21
    • 2021-07-16
    • 1970-01-01
    • 2015-06-05
    • 2019-01-16
    • 1970-01-01
    • 2019-02-09
    • 2019-11-08
    • 2017-07-25
    相关资源
    最近更新 更多