0

I am working with Python and the Django framework. When I want to use hyperlinks , I shouldn't write the full URL manually right ? I have to use some function that returns domain names , and concatenate the path to it manually. So, how can I get the domain name ?
Like:

http://www.domain.com/path/to/file.ext

For this, I want to write :

"http://"+somefunction()+"/path/to/file.ext" 

Is there an equivalent of $_SERVER['HTTP_URI'] in Python.

1
  • @ramesh kumar This might depend on your Apache / Nginx HTTP configuration also. Commented Apr 29, 2012 at 19:41

3 Answers 3

2

For original host of current request, you could use request.get_host() or directly access request['HTTP_HOST'].

Sign up to request clarification or add additional context in comments.

Comments

0

For your needs django offers Sites framefork

>>> from django.contrib.sites.models import Site
>>> Site.objects.get_current().domain
'example.com'
>>> 'http://%s/path/to/file.ext' % Site.objects.get_current().domain
'http://example.com/path/to/file.ext'

Comments

0

Not answering your question directly, but Django has a host of ways of handling URL construction for you, so you don't need to hardcode things.

Inside your Python code:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

def my_redir_view(request):
    return HttpResponseRedirect(reverse('another_view'))

Inside a template:

<a href="{% url 'logout' %}">Logout</a>

Building absolute URLs (on the relatively rare occasions they're needed):

redirect_uri = request.build_absolute_uri(reverse('openid_auth'))

Generally you don't want to be doing manual URL construction - the above methods are your friends.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.