Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

python - Passing data between pages in a redirect() function in Google App Engine

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...