gpt4 book ai didi

python - Flask wtforms 自己的风格

转载 作者:太空宇宙 更新时间:2023-11-03 20:37:36 25 4
gpt4 key购买 nike

我想用flask注册:

@app.route('/register/', methods=['GET','POST'])
def register():
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
username = form.username.data
mail = form.mail.data
password = sha256_crypt.encrypt((str(form.password.data)))
c, conn = dbconnect.conn()
x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
if int(x) > 0:
flash("This username is already taken. Please choose another!")
return render_template("register.html", form=form, nav=8)
else:
c.execute("INSERT INTO users (username, password, mail, credits, settings, rank, items) VALUES (%s, %s, %s, %s, %s, %s, %s)",
thwart(username), thwart(password), thwart(mail), thwart(100), thwart(""), thwart(1), thwart(""))
conn.commit()
flash("You are successfully registered!")
c.close()
conn.close()
gc.collect()
session['logged_in'] = True
session['username'] = username
return redirect(url_for('dashboard'))
return render_template("register.html", form=form, nav=8)

如您所见,我正在使用 wtforms。问题是,我的注册页面是:

{% extends "default.html" %}

{% block body %}

<h4>Register</h4>
<form action="" method="post">
{% from "_formhelpers.html" import render_field %}
{{ render_field(form.username) }}
{{ render_field(form.mail) }}
{{ render_field(form.password) }}
<p><input class="btn btn-primary form-control" type="submit" value="Register"></p>
</form>

{% endblock %}

所以 render_field 定义如下:

{% macro render_field(field) %}
{{ field(**kwargs)|safe }}
{% endmacro %}

但是文本框只是默认样式,所以没有占位符也没有样式。我正在使用 Bootstrap 并希望它们的格式如下:

<p><input type="text" class="form-control" placeholder="{{ field.label }}" name="{{ field.label }}" value=""></p>

如何设置它们的样式?

最佳答案

您可以将 class_ kwargs 传递到 render_field 中的 field() 调用中。

{% macro render_field(field) %}
{{ field(class_="form-control", **kwargs) }}
{% endmacro %}

关于python - Flask wtforms 自己的风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30936082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com