How to get the current user in the HTML page to insert in the Flask Python database

Asked

Viewed 88 times

0

Hello I need to get the user who is logged in to the system for when he does an Update or Insert in the database enter his record, currently in the template I can already get the user name with this command

 {{ current_user.name }}

However when the user calls a route I would like to take this user to enter in the register of manipulations of the database, today I am using python and Flask in my application, could give me a help ?

Below the View code where I can catch the current user, but I need his data when calling the next route

{% extends "base.html" %}
{% block conteudo %}

{% include 'components/notification.html' %}

<div class="container">
    <div class="row">
      <div class="col-sm-3">
        <span><p>Olá <label  name="usuario">{{ current_user.name }}</label>, escolha o arquivo com as informações para começarmos </p></span>
      </div>
      <div class="col-sm-6">
              <form action = "/uploader" method = "POST" enctype = "multipart/form-data">
              <input type = "file" name = "file" />
              <input type = "submit"/>
              </form>
      </div>
      <div class="col-sm-3">

      </div>
    </div>
  </div>
{% endblock %}
  • Rodrigo, please put the code you are trying to use on view/controller

  • Hello, I just updated my post, what I wanted and take the current user and try to pass this information to the Route /Uploader , I put the {{current_user.name}} as parameter but it does not work, in View it works ok , takes the user data that is currently logged in.

1 answer

2


The flask-login works with the current_user in global scope in the application, just as you use current_user in the template, you can use it in your route '/Uploader', just you import the current_user of own flask-login, see the example below, taking into account that the user is logged in:

from flask_login import current_user

@app.route('/uploader', methods = ['GET','POST'])
def uploader():
    print(current_user.nome)
  • thanks so much for the help, it worked out here, I would like to take advantage and ask for a course or documentation indication to improve my knowledge in the use of flask resources and know if for python web this would be the best option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.