在Google App Engine的redirect()函数中的页面之间传递数据 [英] Passing data between pages in a redirect() function in Google App Engine

查看:96
本文介绍了在Google App Engine的redirect()函数中的页面之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GAE构建一个简单的博客,并且制作了下面的代码(我删除了与此问题无关的部分):

 #将模板载入JINJA环境
template_dir = os.path.join(os.path.dirname(__ file__),'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),autoescape = True)

#HELPER FUNCTION
def render_str(template,** params):
t = jinja_env。 get_template(模板)
返回t.render(params)

#GOOGLE DATASTORE数据库
类条目(db.Model):
title = db.StringProperty = True)
body = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)

#处理函数$ b $ class SignUp(webapp2 .RequestHandler):
def get(self):
self.response.write(render_str('signup.html'))

def post(self):
have_e rror = False
username = self.request.get('username')
password = self.request.get('password')
verify = self.request.get('verify' )
email = self.request.get('email')

params = dict(username = username,email = email)

如果不是valid_username(username ):
params ['error_username'] =这不是一个有效的用户名。
have_error = True

如果不是valid_password(密码):
params ['error_password'] =这不是有效的密码。
have_error = True
elif密码!=验证:
params ['error_verify'] =您的密码不匹配。
have_error = True

如果不是valid_email(email):
params ['error_email'] =这不是一个有效的电子邮件。
have_error = True

pwhash = make_secure_val(密码)
self.response.headers.add_header('Set-Cookie','uid:%s'%str(pwhash) )

如果have_error:
self.response.write(render_str('signup.html',** params))
else:
self.redirect(' / welcome')
$ b $ class Welcome(webapp2.RequestHandler):
def get(self):
self.response.write(render_str('welcome.html'))

#APP hANDLERS
app = webapp2.WSGIApplication([('/',MainPage),
('/ newpost',NewPost),
('/ newpost /(\ d +)',Permalink),
('/ signup',SignUp),
('/ welcome',Welcome)
],debug = True)

signup.html 是一种简单的形式, 用户名密码密码再次验证并选择电子邮件
$ b

make_secure_val()就是一个哈希函数,它返回一个 HMAC 参数字符串的散列版本的格式为参数| HMAC(参数)



这里是我的问题:一旦用户注册,我想重定向到另一个URL / welcome ,这样我就可以使用 redirect()函数。但我也想在欢迎页面上打印输入到表单中的用户用户名。我知道如何在 redirect()中传递变量的唯一方法是通过 GET 传入URL。但我不希望URL显示用户名。我想将它作为模板变量传递,如 render_str()。但是如果我在 SignUp POST 方法中使用 render_str() c>,URL仍然是 / signup



如何将数据传递给 redirect()

解决方案

您可以使用 webapp2会话,以更加灵活和安全的方式在多个请求之间共享数据,而无需将其编码为网址。



查看本问答示例:在Google应用引擎中的Webapp2会话


I'm trying to build a simple blog using GAE and I've made the following code (I've deleted the parts which are not related to this question) :

# LOADING THE TEMPLATE INTO THE JINJA ENVIRONMENT
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)

# HELPER FUNCTION
def render_str(template, **params):
    t = jinja_env.get_template(template)
    return t.render(params)

# GOOGLE DATASTORE DATABASE
class Entries(db.Model):
    title = db.StringProperty(required = True)
    body = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)

# HANDLER FUNCTIONS
class SignUp(webapp2.RequestHandler):
    def get(self):
        self.response.write(render_str('signup.html'))

    def post(self):
        have_error = False
        username = self.request.get('username')
        password = self.request.get('password')
        verify = self.request.get('verify')
        email = self.request.get('email')

        params = dict(username = username, email = email)

        if not valid_username(username):
            params['error_username'] = "That's not a valid username."
            have_error = True

        if not valid_password(password):
            params['error_password'] = "That wasn't a valid password."
            have_error = True
        elif password != verify:
            params['error_verify'] = "Your passwords didn't match."
            have_error = True

        if not valid_email(email):
            params['error_email'] = "That's not a valid email."
            have_error = True

        pwhash = make_secure_val(password)
        self.response.headers.add_header('Set-Cookie', 'uid: %s' % str(pwhash))

        if have_error:
            self.response.write(render_str('signup.html', **params))
        else:
            self.redirect('/welcome')

class Welcome(webapp2.RequestHandler):
    def get(self):
        self.response.write(render_str('welcome.html'))

# APP HANDLERS
app = webapp2.WSGIApplication([('/', MainPage),
                            ('/newpost', NewPost),
                            ('/newpost/(\d+)', Permalink),
                            ('/signup', SignUp),
                            ('/welcome', Welcome)
                            ], debug=True)

signup.html is a just simple form that takes in the username, password, password again to verify and an optional email.

make_secure_val() is just a hashing function that returns an HMAC hashed version of the argument string in the format argument|HMAC(argument).

So, here's my question: Once the user signs up, I want a redirect to another URL /welcome, thus making me use the redirect() function. But I also want to print the username the user inputted into the form on the welcome page. The only way I know how to pass variables in a redirect() is to pass into the URL through GET. But I don't want the URL to display the username. I want to pass it as a template variable like in render_str(). But if I use render_str() in the POST method of SignUp, the URL will still be /signup.

How do I pass in the data to a redirect()?

解决方案

You could use webapp2 sessions to share data across multiple requests in a much more flexible and secure manner, without encoding it into URLs.

See an example this Q&A: Webapp2 Sessions in Google app engine

这篇关于在Google App Engine的redirect()函数中的页面之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆