1 August 2010

This tutorial shows you how to get your domain name(base url) from address bar in Google AppEngine, using self and urlparse

This is simple tutorial, more like a snippet. That shows you how you can get your domain from address bar. For example you are browsing our site and your URL in address bar is "/tutorial/get-self-base-url-appengine-urlparse". With this script you can get the domain out of it, that would be "www.browse-tutorials.com".
Base URL:
This is a part of my python file of my Google AppEngine application. I wanted to use base url in my view and this is how I do it. Don't forget to add the library.
Source code viewer
  1. from urlparse import urlparse
  2. ###
  3. def get(self):
  4. template_values = {
  5. #This is the part where we get base_url from
  6. #self and using urlparse we get the domain name.
  7. 'base_url': urlparse(self.request.url).netloc,
  8. }
  9.  
  10. path = os.path.join(os.path.dirname(__file__), 'template/index_card.html')
  11. self.response.out.write(template.render(path, template_values))
  12. ###
  13.  
Programming Language: Python
The same thing in Python console.
Source code viewer
  1. user_name@computer_name:~$ python
  2. Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
  3. [GCC 4.4.3] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> from urlparse import urlparse
  6. >>> url = '/tutorial/get-self-base-url-appengine-urlparse'
  7. >>> url
  8. '/tutorial/get-self-base-url-appengine-urlparse'
  9. >>> url_array = urlparse(url)
  10. >>> url_array
  11. ParseResult(scheme='http', netloc='www.browse-tutorials.net', path='/tutorial/get-self-base-url-appengine-urlparse', params='', query='', fragment='')
  12. >>> base_url = url_array.netloc
  13. >>> base_url
  14. 'www.browse-tutorials.net'
  15. >>>
Programming Language: Text