我正在尝试使用GAE构建一个简单的博客,我已经制作了以下代码(我删除了与此问题无关的部分):
# 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是一个简单的表单,它再次接收用户名,密码,密码以验证和可选的电子邮件.
make_secure_val()只是一个散列函数,它返回格式参数| HMAC(参数)中参数字符串的HMAC哈希版本.
所以,这是我的问题:一旦用户注册,我想重定向到另一个URL / welcome,从而使我使用redirect()函数.但我还想打印用户在欢迎页面上输入表单的用户名.我知道如何在redirect()中传递变量的唯一方法是通过GET传递到URL.但我不希望URL显示用户名.我想将它作为模板变量传递,就像在render_str()中一样.但是如果我在SignUp的POST方法中使用render_str(),则URL仍将是/ signup.
如何将数据传递给重定向()?