vaibhav1312
vaibhav1312

Reputation: 893

unable to get redirect work in google app Engine

I am beginner in Google App engine, and trying to go through Google App HelloWorld app. redirect url does not work while using templates.

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

It redirects but should show exact url in urlbar like

http://localhost:8080/?guestbook_name=some_name

whereas it displays like

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 file looks like

 <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>

Upvotes: 0

Views: 845

Answers (3)

flyingfoxlee
flyingfoxlee

Reputation: 1794

I encounter a similar problem while trying gae, the official site https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates has changed the related code just like Anthony metioned in his post. But here are some problems too:

  1. In the template_values variable in class MainPage, guestbook_name was encoded by urllib.urlencode which will fail since this function takes a dict or a tuple as a variable rather than a string, urllib.quote_plus should be used instead.
  2. Two forms in index.html shares the same encoded variable guestbook_name, which is not pretty if your language is non-English, in which case some %2F like strings will appears in the content, so, my solution is send a extra non encoded guestbook_name for the second form.

Upvotes: 0

Anthony
Anthony

Reputation: 141

In your index.html there is no input that sets the guestbook name. For an empty key, webapp2 returns an empty string for self.request.get('key_name') instead of None as you might expect. You can see this by looking at the datastore, all your stored greetings will have an empty guestbook_name.

To get the guestbook_name set your template values in the get method to

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

and in your 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>

That will add the guestbook_name to data you send in the POST, so you will be able to use it in your handler.

Upvotes: 1

marcadian
marcadian

Reputation: 2618

Seems your guestbook_name is '' (empty string)

Upvotes: 2

Related Questions