开发者

How do I pass template values when doing a redirect?

开发者 https://www.devze.com 2023-03-31 03:18 出处:网络
I use the blobstoreuploadhandler and hence must return a self.redirect but I need to pass values to my template. How can I do it? If I can\'t use template values then I suppose I can use session varia

I use the blobstoreuploadhandler and hence must return a self.redirect but I need to pass values to my template. How can I do it? If I can't use template values then I suppose I can use session variables and I've included the beaker session library but I can't understand how to access the session variables in django template. Any idea how I should do it?

I use default builtin django with google app engine and I can access session variables with a request handler but I don't understand how to do it in templates:

class Sessiontest(webapp.RequestHandler):
    def get(self):
        # Get the session object from the environ
        self.session = self.request.environ['beaker.session']

        # Check to see if a value is in the session
        if 'counter' in self.session:
            counter = self.session['counter'] + 1
            self.session['counter'] = counter

        else:
            self.session['counter'] = 1
            counter = 1
        self.session.save()
        self.response.out.write('counter: %d' % counter)

Thanks

Up开发者_如何学Pythondate/edit: My problem is almost exactly like this Accessing session variable in Django template with Google App Engine (Webapp) - Python but with the library beaker instead of gaeutilities

Update: Here's some of the code. we see that using HTTP GET to pass the values won't be very good since there's an anti-spam test that should hide the values:

def post(self, view):
    message = ''
    challenge = self.request.get('recaptcha_challenge_field').encode('utf-8')
    response  = self.request.get('recaptcha_response_field').encode('utf-8')
    remoteip  = os.environ['REMOTE_ADDR']
    cResponse = captcha.submit(
                 challenge,
                 response,
                 CAPTCHA_PRV_KEY,
                 remoteip)
    if cResponse.is_valid:
        isHuman=True
    else:#failed anti-spam test and can try again
        isHuman=False
        #Reprint the form
        import util
        template_values = {'isHuman':isHuman,'user' : users.get_current_user(),}
        template_values.update(dict(current_user=self.current_user, facebook_app_id=FACEBOOK_APP_ID))
        template_values.update(dict(capture=captcha.displayhtml(public_key = CAPTCHA_PUB_KEY, use_ssl = False, error = None)))
        path = os.path.join(os.path.dirname(__file__), 'market', 'market_insert.html')
        self.redirect("/ai") # Here the values aren't passed on and I must make a redirect


If you are doing a redirect you might have to redirect with the variables that you wish to keep in the GET string. So you redirect from

/myview/

to

/myview2/?variable1=value

However, I think you should really look to see why you are doing redirects. I tend to do them after a POST to a form, and if the user needs to be logged on, I redirect to a login screen with

/authentication/login/?next=/view/they/wanted/to/see

Otherwise you could keep things in cookies but its not the best way to proceed.


How about letting your class inherit from multiple classes, both requesthandler class and blobstoreuploadhandler, in that way you can both render your template with values with the functions in the requesthandler, and use the functions in blobstoreuploadhandler?

A class definition with multiple base classes looks as follows:

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    ...
    <statement-N>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号