Home > Enterprise >  Updating values with WTFORMS and SQAlchemy in flask
Updating values with WTFORMS and SQAlchemy in flask

Time:11-05

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

CodePudding user response:

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)
  • Related