Flask display user register, or already login in every template of each module

旧街凉风 提交于 2021-01-07 07:45:13

问题


I'm learning flask and im up to the blueprint section. First of all, i have the header.html which is included by base.html, and the route for index is below:

@app.route('/')
def index():
  return render_template("index.html", is_auth=session[is_authenticated], username=session[username])

header.html:

{% if is_auth %}
    Welcome {{ username }}</font>,
{% else %}
    <a href="{{ url_for('users.register') }}"> Register / Login</a></li>
{% endif %}

base.html is simple:

{% block header %}
{% include 'header.html' %}
{% endblock %}

{% block content %}
{% endblock %}  

{% block footer %}
{% include 'footer.html' %}
{% endblock %}

Everything works fine, the header will ask to register if not logged in, otherwise display "Welcome...".

My problem is that i'm using blueprints for 2 modules which are "users" and "books" so they have 2 following subdir: /users/ and /books/. For each module it will extend the 'content' block from base.html

However, the thing is that if I have a route for either profile, rank, logout etc ... I then need to send out the session[is_authenticated] and session[username] to the template in order to display the header correctly. What I mean is like:

@mod.route('/profile/')
@requires_login
def profile():
  # do something here
  return render_template("users/profile.html", is_auth=session[is_authenticated], username=session[username])

@mod.route('/rank/')
@requires_login
def rank():
  # do something here
  return render_template("users/rank.html", is_auth=session[is_authenticated], username=session[username])

@mod.route('/register/')
def register():
  # do something here
  return render_template("users/register.html", form=forms, is_auth=session[is_authenticated], username=session[username])

The code is very redundant because I have to send out same thing over and over again. Are there any efficient ways or better way to deal with this problem?

Thank you so much and have a nice daY!


回答1:


sounds like you should use flask-login to make your life easier. if you look on the github, you'll see that they do what you're trying to do already

:param add_context_processor: Whether to add a context processor to
  the app that adds a `current_user` variable to the template.
  Defaults to ``True``.

Quick version of getting it set up for your purposes:

start by initializing flask-login on your app

from flask.ext.login import LoginManager
login_manager = LoginManager()

def create_app(config=None):
  # ...
  login_manager.init_app(app)
  return app

then, add a user_loader in your models, or wherever your model of a user is

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

then, on your login view, you can simply add a few lines to grab the user and use flask-login's login_user function. This example assumes it came in from a WTForm and uses the form's data and SQLAlchemy. User is my model. Very basic example.

from ..models import User
# ...
u = User.query.filter_by(email=form.name.data).first()
if u is not None:
  login_user(u)

on your templates, there will be a global for current_user with flask-login. you can simply use {{ current_user.is_authenticated }} to check if the current user is there.

{% if current_user.is_authenticated %}
  <h1>Hi, {{ current_user.username }}</h1>
{% else %}
  <a href="{{ url_for(user.login) }}">why aren't you logged in?</a>
{% endif %}


来源:https://stackoverflow.com/questions/24922831/flask-display-user-register-or-already-login-in-every-template-of-each-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!