0

Hello I am making a blogging website with flask, flask-sqlalchemy, and flask-login. I am trying to figure out a way to add optional data such as a role after the account creation. This is as far as I have gotten:

@app.route('/addpinfo', methods=["POST", "GET"])
@login_required
def accountinfo():
    if request.method == "POST":
        username = current_user.username
        job = request.form.get("role")
        user_job = Loginbase(job=job)
        db.session.add(user_job)
        db.session.commit()
        return redirect(url_for("homebase"))
    else:
        return render_template("accountspecs.html")

Here is what the HTML looks like:

<form method="post">
    <script src="{{url_for('static', filename='js/accountspecs.js')}}"></script>
    <input class="dissapointment form-box" type="text" placeholder="Write a hobby or job title"name="role"><br>
    <button class="cool-sub" type="submit" name="submit">Add Role Info</button> 
</form>

Does anyone know where I can go from here? I have been having trouble trying to find a way to add that element to the account the user is currently using.

Thank you for any help ahead of time.

  • Basically, you need to query for the current user, and then update your desired attributes. See [here](https://stackoverflow.com/questions/6699360/flask-sqlalchemy-update-a-rows-information) for more details. – Patrick Yoder Mar 31 '22 at 07:01
  • Thank you so much can't believe I didn't think of that! – Alfangeist Apr 02 '22 at 02:45

1 Answers1

0

The solution that worked with my code was:

@app.route('/addpinfo', methods=["POST", "GET"])
@login_required
def accountinfo():
    if request.method == "POST":
        user = current_user
        job = request.form.get("role")
        user.job = job
        db.session.commit()
        return redirect(url_for("homebase"))
    else:
        return render_template("accountspecs.html")

This allowed for my role system to work. I thank Patrick for all of their help.