我是 Google App 引擎的初学者,并试图通过 Google App HelloWorld 应用程序。使用模板时重定向 url 不起作用。
self.redirect('/?'+urllib.urlencode({'guestbook_name':guestbook_name}))
它会重定向,但应该在 urlbar 中显示确切的 url,例如
http://localhost:8080/?guestbook_name=some_name
而它显示像
http://localhost:8080/?guestbook_name=
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name=self.request.get('guestbook_name')
greetings_query = Greeting.all().ancestor(
guestbook_key(guestbook_name)).order('-date')
greetings = greetings_query.fetch(10)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'greetings': greetings,
'url': url,
'url_linktext': url_linktext,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name')
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user().nickname()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
Index.html 文件看起来像
<html>
<body>
{% for greeting in greetings %}
{% if greeting.author %}
<b>{{ greeting.author }}</b> wrote:
{% else %}
An anonymous person wrote:
{% endif %}
<blockquote>{{ greeting.content|escape }}</blockquote>
{% endfor %}
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<a href="{{ url }}">{{ url_linktext }}</a>
</body>
</html>