无法在Google App Engine中实现重定向

0 投票
3 回答
842 浏览
提问于 2025-04-17 19:48

我刚开始学习Google App Engine,正在尝试了解Google App的HelloWorld应用。但是在使用模板的时候,重定向的链接不太好使。

self.redirect('/?'+urllib.urlencode({'guestbook_name':guestbook_name}))

它确实会重定向,但在地址栏中应该显示准确的链接,比如说

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>

3 个回答

0

我在尝试使用gae的时候遇到了类似的问题,官方站点 https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates 上的相关代码已经像Anthony在他的帖子中提到的那样发生了变化。 不过这里也有一些问题:

  1. 在MainPage类的template_values变量中,guestbook_name 是通过 urllib.urlencode 编码的,但这会失败,因为这个函数需要一个字典或元组作为参数,而不是字符串,所以应该使用 urllib.quote_plus
  2. 在index.html中,有两个表单使用了相同的编码变量 guestbook_name,如果你的语言不是英语,这样就不好了,因为内容中会出现一些像 %2F 这样的字符串。所以,我的解决办法是为第二个表单发送一个额外的未编码的 guestbook_name
1

在你的index.html文件中,没有输入框来设置留言簿的名称。对于一个空的键,webapp2会返回一个空字符串给self.request.get('key_name'),而不是你可能期待的None。你可以通过查看数据存储来验证这一点,所有你存储的问候语都会有一个空的guestbook_name。

要获取guestbook_name,你需要在get方法中设置你的模板值为

template_values = {
    'greetings': greetings,
    'guestbook_name': guestbook_name,
    'url': url,
    'url_linktext': url_linktext,
}

然后在你的index.html中

<form action="/sign" method="post">
  <div><textarea name="content" rows="3" cols="60"></textarea></div>
  <div><input type="hidden" name="guestbook_name" value="{{guestbook_name}}"></div>
  <div><input type="submit" value="Sign Guestbook"></div>
</form>

这样会把guestbook_name添加到你在POST请求中发送的数据里,这样你就可以在处理程序中使用它了。

2

看起来你的留言簿名称是空的(就是没有内容)

撰写回答