Question

I was wondering if it is possible to redirect from within a render method in twisted web.

I have tried the various ways of redirecting and have only found it documented when used in the getChild method.

Basically I am checking to see if a user is logged in and if it isn't then forward the user onto a different Resource.

    def render_GET(self, request):
        player = getPlayer(request)
        if player.loggedIn():
            return Redirect("play")
        else:
            return Redirect("login")

I have looked all over but I am unable to find a well documented example on twisted documents.

I am not sure if I need to change my approach to this and I am new to twisted web. Any help would be greatly appreciated.

Thank you

Was it helpful?

Solution

Apologies

Upon further investigation and by fault of my own I had overlooked the "redirectTo" method of twisted.web.util

This has worked for me perfectly for me. Just thought I would post this here in case anyone else is looking for the same answer.

    from twisted.web.util import redirectTo

    def render_GET(self, request):
        player = getPlayer(request)
        if player.loggedIn():
            return redirectTo("play")
        else:
            return redirectTo("login")

Kind regards Joe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top