Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change flash messages. #324

Open
feerbau opened this issue Oct 17, 2020 · 1 comment
Open

How to change flash messages. #324

feerbau opened this issue Oct 17, 2020 · 1 comment

Comments

@feerbau
Copy link

feerbau commented Oct 17, 2020

Hello i would like to know if it's possible to change default flask-user flash messages.
For example the ones in "user_manager__views.py"
When i use @login_required it redirects the user to the login page and shows this message "You must be signed in to access {here_the_url}."

What can i do if i want to show another message? Is it possible?

@Chaostheorie
Copy link

Use a custom user manager. You will be able to change the function used for the view below is an example that should be enough to give you an idea on where to start. Haven't tested it but should be able to do the job.

from flask_user.user_manager import UserManager
from flask import redirect, flash

class CustomUserManager(UserManager):
    def unauthenticated_view(self):
        """ Prepare a Flash message and redirect to USER_UNAUTHENTICATED_ENDPOINT"""
        # Prepare Flash message
        url = request.url
        flash(_("You must be signed in to access '%(url)s'.", url=url), 'error') # Insert your own message

        # Redirect to USER_UNAUTHENTICATED_ENDPOINT
        safe_next_url = self.make_safe_url(url)
        return redirect(self._endpoint_url(self.USER_UNAUTHENTICATED_ENDPOINT)+'?next='+quote(safe_next_url))

user_manager = CustomUserManager(app, db, User)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants