我想用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 %}


但是文本框只是默认样式,因此没有占位符也没有样式。我正在使用引导程序,并希望它们的格式如下:

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


如何样式?

最佳答案

这是我们如何写它

{% macro render_field(name, class='class-name', value='', type='text') -%}
<input type="{{ type }}" name="{{ name }}" class="{{ class }}"
value="{{ value }}"/>
{%- endmacro %}


你的情况

{% macro render_field(name, class='form-control', type='text') %}
{{ field(**kwargs | safe) }}
{% endmacro %}


渲染字段时,您始终可以应用自己的样式。

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

10-10 20:08