使用来自 SQLAlchemy 对象的数据在烧瓶中预填充 WTforms [英] Pre-Populate a WTforms in flask, with data from a SQLAlchemy object

查看:25
本文介绍了使用来自 SQLAlchemy 对象的数据在烧瓶中预填充 WTforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Flask 框架相当陌生,正在为 Web 门户创建编辑配置文件页面.我陷入困境,无法自动填写表单.

这是我的表单类:

class EditProfile(Form):username = TextField('用户名', [Required()])email = TextField('Email', [Required()])about = TextAreaField('About', [Required()])website = TextField('网站', [Required()])

这是我计算表单的函数.

def editprofile(昵称 = 无):如果 g.fas_user['username'] == 昵称或 request.method == 'POST':表单 = EditProfile()form_action = url_for('profile.editprofile')如果 request.method == 'POST' 和 form.validate():如果 form.username.data == 昵称:查询 = EditProfile(form.username.data,表单.email.data,form.about.data,表单.网站.数据,)打印查询#debugdb.session.add(查询)db.session.commit()flash('用户更新')打印添加"返回(url_for('profile.editprofile'))return render_template('profile/add.html', form=form,form_action=form_action, title="更新配置文件")别的:返回未经授权"

我的表单html模板是表单:

{% 扩展 "base.html" %}{% 块标题 %}{{ 标题 }}{% 结束块 %}{% 块内容 %}{% from "_formhelpers.html" import render_field %}<div id="编辑个人资料"><h2>{{title}}</h2><form method="post" action="{{ form_action }}"><字段集><图例></图例>{{ render_field(form.username) }}{{render_field(form.email)}}{{ render_field(form.about )}}{{ render_field(form.website) }}</fieldset><input type="submit" class="button" value="Save"/></表单>

{% 结束块 %}


我有一个用户类的对象.我想从该对象预填充此表单.如何预填充表单中的值.我正在尝试在这里实现编辑配置文件功能.

解决方案

您需要在创建对象时将其传递给表单.

form = EditProfile(obj=user) # 或者任何你的对象被称为

你会遇到一些麻烦

 query = EditProfile(form.username.data,表单.email.data,form.about.data,表单.网站.数据,)db.session.add(查询)

它会创建您的 EditProfile 表单的新实例.然后尝试将其添加到会话中.会议需要模型,而不是表单.

相反,在验证表单后,您可以将其值与对象相关联.

form.populate_obj(user) # 或者你的对象叫什么

因为您的对象已经加载,所以您无需将其添加到会话中.您可以删除 db.session.add(query) 并调用 db.session.commit().

I am fairly new to flask framework and was creating an edit profile page for a webportal. I am stuck at a point and am unable to autofill a form.

Here is my form class :

class EditProfile(Form):

    username = TextField('Username', [Required()])
    email = TextField('Email', [Required()])
    about = TextAreaField('About', [Required()])
    website = TextField('Website', [Required()])

This is my function that evaluates the form.

def editprofile(nickname = None):
    if g.fas_user['username'] == nickname  or request.method == 'POST':
        form = EditProfile()
        form_action = url_for('profile.editprofile')
        if request.method == 'POST' and form.validate():
            if form.username.data == nickname : 
              query = EditProfile(form.username.data,
                                 form.email.data,
                                 form.about.data,
                                 form.website.data,
                                 )
              print query #debug
              db.session.add(query)
              db.session.commit()
              flash('User Updated')
              print "added"
            return(url_for('profile.editprofile'))
        return render_template('profile/add.html', form=form,
                               form_action=form_action, title="Update Profile")
    else:
        return "Unauthorised"

And my html template for form is form is :

{% extends "base.html" %}
    {% block title %}
        {{ title }}
    {% endblock %}
    {% block content %}
    {% from "_formhelpers.html" import render_field %}
    <div id="Edit Profile">
        <h2>{{  title  }}</h2>
        <form method="post" action="{{ form_action }}">
            <fieldset>
                <legend></legend>
                {{ render_field(form.username) }}
                {{ render_field(form.email)}}
                {{ render_field(form.about )}}
                {{ render_field(form.website) }}
            </fieldset>
        <input type="submit" class="button" value="Save"/>
    </form>
    </div>
    {% endblock %}


I have an object, of user class. And from that object I want to prefill this form.How can I prepopulate the values in the form. I am trying to implement the edit profile functionality here.

解决方案

You need to pass your object to the form when you create it.

form = EditProfile(obj=user)  # or whatever your object is called

You're going to run into some trouble with

          query = EditProfile(form.username.data,
                             form.email.data,
                             form.about.data,
                             form.website.data,
                             )
          db.session.add(query)

It creates a new instance of your EditProfile form. You then try to add it to the session. The session wants models, not forms.

Instead, after validating the form, you can associate its values with the object.

form.populate_obj(user)  # or whatever your object is called

Because your object was already loaded, you won't need to add it to the session. You can remove db.session.add(query) and just call db.session.commit().

这篇关于使用来自 SQLAlchemy 对象的数据在烧瓶中预填充 WTforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆