简体   繁体   中英

Updating values with WTFORMS and SQAlchemy in flask

I have a website. I want to allow people to easily update there email address by just typing it in a form. This is my current code as an attempt, but everything I tried will not work. I already have a column for their emails which is just named email.

@app.route('/dashboard/settings', methods=['GET', 'POST'])
@login_required
def settings():
    form = EmailForm()
    if form.validate_on_submit():
        current_user.email = form.email.data
        session.commit()
        return redirect("dashboard/settings")
    return render_template("settings.html", form=form)

These are my plugins

from flask import Flask, render_template, session, abort, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_bcrypt import Bcrypt
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView  
from flask_security import roles_required

The issue is that I was not finding the users info that I wanted to replace. I did a query to find the current users row, then I edited the email to the data that the form recieved.

def settings():
form = EmailForm()
if form.is_submitted():
    user = User.query.filter_by(username=current_user.username).first()
    user.email = form.email.data
    db.session.commit()
    return redirect("/dashboard/settings")
return render_template("settings.html", form=form)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM