我正在尝试使用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
是一个简单的表单,可以再次使用username
,password
,password
进行验证,并选择email
。
make_secure_val()
只是一个散列函数,它以[{1}}格式返回参数字符串的HMAC
散列版本。
所以,这是我的问题:用户注册后,我想要重定向到另一个网址argument|HMAC(argument)
,从而使我使用/welcome
功能。但我还想在欢迎页面上打印输入到表单中的redirect()
用户。我知道如何在username
中传递变量的唯一方法是通过redirect()
传递到网址。但我不希望URL显示用户名。我想将其作为模板变量传递,如GET
中所示。但是,如果我在render_str()
的{{1}}方法中使用render_str()
,则网址仍为POST
。
如何将数据传递到SignUp
?