-1

I have been trying to get the request url as follows

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        print self.request.get('url')

app = webapp2.WSGIApplication([('/.*', MainPage)], debug=True)

when the request is

http://localhost:8080/index.html

it gives me something like

Status: 200 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Content-Length: 70

what I need to get is something like

index.html

edit: so that I can check the string and display the correct html/template file accordingly.

I have already checked Request documentation and tried many alternatives yet I can't seem to find a solution. I'm quite new to web development. What am I missing?

none
  • 11,793
  • 9
  • 51
  • 87

3 Answers3

6

you should start here: https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld

you are not using a template or a templating module/engine so its irrelevant what path you are accessing because you are mapping everything with /.*

use self.response.write() not print.
again its better for you if you start reading the documentation from the beginning before checking out the request class.

EDIT:

if you want to get the urlpath in the requesthandler and serve different templates based on the urlpath then do something like this:

def get(self):
    if self.request.path == '/foo':
        # here i write something out
        # but you would serve a template
        self.response.write('urlpath is /foo')
    elif self.request.path == '/bar':
        self.response.write('urlpath is /bar')
    else:
        self.response.write('urlpath is %s' %self.request.path)

a better approach would be to have multiple ReqeustHandlers and map them in the WSGIApplication:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/foo', FooHandler),
                               ('/bar', BarHandler),
                               ('/.*', CatchEverythingElseHandler)], debug=True)
aschmid00
  • 7,038
  • 2
  • 47
  • 66
  • I have already read the guide but there is nothing related to my question there. It is irrelevant what path I'm accessing in my toy example but in the end I'm planning to use templates or static html pages according to the requested url. – none Jul 21 '12 at 19:08
  • 1
    i think your question is really unclear. what do you mean with `what I need to get is something like index.html`? you want the page to render a template? do you want `index.html` written in the page? i suggest you update your question by specifically asking what you need to do instead of 'toy examples'. – aschmid00 Jul 21 '12 at 21:38
  • take a look here https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates – aschmid00 Jul 21 '12 at 21:50
  • told you I have already read that tutorial a few times. there is only one template in that example. what if I have `index1.html` and `index2.html` and I need to decide which one to display according to the requested url? – none Jul 21 '12 at 21:54
  • if you want index1.html or index2.html output then why are you printing request. You should be examining the request object as per the docs as previous people have suggested. Spoecifically look at https://developers.google.com/appengine/docs/python/tools/webapp/requestclass#Request on how to interact with the request object. Once you have got the url from request you can then make a determination about what template to load etc. The output you are currently getting is what you get when request is printed or repr(request) is called. – Tim Hoffman Jul 21 '12 at 23:34
  • dude there are no previous people but only one person and the documentation you linked is the one I suggested in my question. I have already read it and clearly I'm not interpreting it correctly. – none Jul 22 '12 at 03:29
  • `once you have got the url from request` is the part I'm trying to achieve. I don't think this is going anywhere useful. – none Jul 22 '12 at 03:31
  • if you need the request path then its `self.request.path`, but thats not what you want to do. what you want is a `RequestHandler` for each urlpath you need to serve. then map the handler to the url in the `WSGIApplication`. everything is in the docs... but your approach is not right, you do not check for the path in the handler (at least not in most cases) but have different logic based on the urlpath. – aschmid00 Jul 22 '12 at 13:13
  • turned out I was mixing `request` and `response` somehow. it's working now. I will also consider the second approach in your edited answer. thanks for the help. – none Jul 22 '12 at 15:21
2

Instead of:

self.request.get('url')

use:

self.request.url

Other options that you might find useful are:

self.request.path
self.request.referer

Try these changes to get the result you are looking for.

sathish_at_madison
  • 823
  • 11
  • 34
Nitish Parkar
  • 2,838
  • 1
  • 19
  • 22
1

You can use this too:

def get(self):
     self.request.GET['name']
     self.request.GET['sender']
Atul
  • 143
  • 10