Alexis
Alexis

Reputation: 25163

Getting the root url in Django

In my view, I want to make a request to mine.com/more/stuff/ from an arbitrary page such as mine.com/lots/of/stuff/to/use or from mine.com. Thus, I can't make this a relative request using ./ or ./../ type things. Do I have to use a context processor to do {{URL_BASE}}more/stuff/? Is there a set way to do this in Django or a best way?

Upvotes: 2

Views: 3043

Answers (2)

MechanisM
MechanisM

Reputation: 906

why don't you use named urls? it's always works. for example {% url 'admin:index' %} always printed as url to admin(in case if you using default django.contrib.admin app). if you'll have in urls.py smth like

url(r'^lots/', Lots.as_view(), name='lots'),

then just use smth like

{% url 'lots' %}

Don't hardcode your urls!

Upvotes: 8

Instead of a relative url, use an absolute url: /

If you're on mine.com/lots/of/stuff/to/use or mine.com, hitting a link with url: /foo/ will both go to mine.com/foo/

Upvotes: 5

Related Questions