hopheady
hopheady

Reputation: 37

Flask trying to call function with a redirect

I’m using Flask with pluggable views. Having some trouble getting a redirect to work. Here is the code.

def some_redirect():
    *condition for redirect*
        return redirect(url)


class SomeClass(View):
    *few methods*

    def dispatch_request(self):
        some_redirect()
        *other stuff*

Essentially all I want to do is check is either a certain value in session exists or if a certain config key is present. If not, I’d like to redirect to an external url. Using some print statements I can see that the code reaches the redirect. But instead of sending me to someurl.com, it continues in dispatch request. I don’t want to use a decorator here because I’m not really modifying any of these methods. I essentially just want to redirect to the given url if those conditions are not met. Any help would be much appreciated.

Upvotes: 1

Views: 623

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600049

You need to return the result of the function:

def dispatch_request(self):
    redirect = redirect_if_unauthorized()
    if redirect:
        return redirect

Upvotes: 3

Related Questions