title: Get the user id in flask
body: <p>Note. Not duplicated of <a href="https://stackoverflow.com/questions/41093661/get-current-user-id-in-flask">Get current user id in Flask</a>.</p>
  
  <p>I use flask_login and flask, so to get the current id its only flask_login.current_user.id. But the problem occurs when I want to create a variable with the command, like, in my app.py:</p>
  
  <pre><code>...
  curr_user = flask_login.current_user.id
  
  def function_a(curr_user):
      os.mkdir(path + '/' + curr_user)
      return 'Path created successfully'
  ...
  </code></pre>
  
  <p>That says:</p>
  
  <blockquote>
    <p>AttributeError: 'NoneType' object has no attribute 'get_id'</p>
  </blockquote>
  
  <p>Only when running (don't need to be called, the only assignment of value to curr_user do the problem)</p>
  
  <p>Tried calling the function w/variable as flask_login.current_user.id and Ive the same. </p>
  
  <p>I think the trouble is because I cant get the id of user in general (in the app.py running instance), and that need context. So, I tried: </p>
  
  <pre><code>with app.test_request_context():
  </code></pre>
  
  <p>before assignment, and I got only None for users and guests.</p>
  
  <p>Edit. I know I can just do:</p>
  
  <pre><code>def function_a():
      os.mkdir(path+'/'+flask_login.current_user.id)
      return 'Path created successfully'
  </code></pre>
  
  <p>But that was only an example, the really need is the assignment of value to a non-local variable with context.</p>