Maddie Graham
Maddie Graham

Reputation: 2177

Receiving the application URL. Django

I need to get the current URL of my application in my views.py. Not the bookmark I am in but the address to the home page (all).

I finded this solution:

url = "{0}://{1}".format(request.scheme, request.get_host())

But i but I think it can be simpler . Not using 'request ....' twice and by obtaining an interpretation of one variable.

Any good suggestions will be appreciated.

Upvotes: 0

Views: 65

Answers (1)

moltarze
moltarze

Reputation: 1501

url = request.build_absolute_uri("/")

See the Django documentation reference:

Returns the absolute URI form of location. If no location is provided, the location will be set to request.get_full_path().

If the location is already an absolute URI, it will not be altered. Otherwise the absolute URI is built using the server variables available in this request.

This seems to be what you are asking for.

Upvotes: 1

Related Questions