base_url() in django

3,457 views
Skip to first unread message

Raitucarp

unread,
Jun 24, 2012, 9:10:08 PM6/24/12
to django...@googlegroups.com
Actually, I was PHP developer, I use codeigniter as framework. In codeigniter, we can call helper url functions base_url() and it will return absolute domain url such:

http://example.com
or
https://example.com

It is automatically, we even don't need to create custom function, because base_url() is a helper function. I call the base_url() function just in views (in django context it will be template).

So, what 'function' should I call in django template to return absolute path domain such base_url() in Codeigniter?

Melvyn Sopacua

unread,
Jun 25, 2012, 9:17:25 AM6/25/12
to django...@googlegroups.com
On 25-6-2012 3:10, Raitucarp wrote:

> http://example.com
> or
> https://example.com

You also do this in your view functions, using the Site framework:
<https://docs.djangoproject.com/en/1.4/ref/contrib/sites/#getting-the-current-domain-for-full-urls>
Then pass it on to template.
--
Melvyn Sopacua


Raitucarp

unread,
Jun 25, 2012, 11:02:24 AM6/25/12
to django...@googlegroups.com
@Melvyn

That's not simple way. I have to write every views to pass it on to template? It's not elegant by the way. uhm Should I create custom template tags to do it in beautiful way?

Because base_url() in codeigniter is a helper. And I don't need write it on to controllers(views in django), and pass it to view (template in django)

Anyone?

Melvyn Sopacua

unread,
Jun 25, 2012, 2:17:45 PM6/25/12
to django...@googlegroups.com
On 25-6-2012 17:02, Raitucarp wrote:
> @Melvyn
>
> That's not simple way. I have to write every views to pass it on to
> template? It's not elegant by the way. uhm Should I create custom template
> tags to do it in beautiful way?

Is this an actual issue? You realize that there's no difference between
/doc/ and http://example.com/doc/ if the current server is
http://example.com/?

See here:
https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url
--
Melvyn Sopacua


Kurtis Mullins

unread,
Jun 25, 2012, 3:20:25 PM6/25/12
to django...@googlegroups.com
Is this an actual issue? You realize that there's no difference between
/doc/ and http://example.com/doc/ if the current server is
http://example.com/?

+1

I'd like to see the use-case where having absolute URLs everywhere is actually necessary. It's not hard to do in certain places where you might actually need it. The only place I see needing absolute URLs is when going from HTTP to HTTPs and for printing out links that people may copy and paste. Even in the former case, a permanent-redirect is better.

Jani Tiainen

unread,
Jun 25, 2012, 4:03:36 PM6/25/12
to django...@googlegroups.com
One actual use-case I have is when sending links in emails, for example confirmations, password resets links etc. And IIRC there is a way to do it in Django it's just not documented.

Personally I resolved it by using configuration variable in settings.py since in my case actual Django installation is behind proxies and absolute url user sees is not the same what Django app server sees.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

Raitucarp

unread,
Jun 26, 2012, 3:58:15 AM6/26/12
to django...@googlegroups.com

Is this an actual issue? You realize that there's no difference between
/doc/ and http://example.com/doc/ if the current server is
http://example.com/?

See here:
https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url

No no, that's not my issue. {% url %} template tag just create relative url that represent the views url. But, what I am going to ask here is create absolute url path. I mean in codeigniter I just call base_url() to create the base url where I am work. In django, it's hard to do that, just it.

Kurtis Mullins

unread,
Jun 26, 2012, 10:12:42 AM6/26/12
to django...@googlegroups.com
We understand the issue. You want to display absolute URL including the hostname and protocol in your templates. That's not difficult to do, you just need to write a custom template tag to do this. There's many ways to go about doing this -- however, if you're only running a single web-site on this Django application, I recommend creating a a base URL configuration option in your settings.py and using that for generating the full URL.

The case we're trying to make is -- why do that? Just like Raitucarp said, using an absolute path provides the same end-result as including the hostname and protocol. There's only a few cases where you'd want to do that -- primarily when trying to create links that will be used externally (for example, a "copy and paste link" or for email templates). 

Also, you mention {% url ... %} does relative paths. It doesn't. It does absolute. It just doesn't include the hostname and protocol. A relative path would not begin with a leading "/".

Good luck!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/cinsOV4C4fMJ.

Kurtis Mullins

unread,
Jun 26, 2012, 10:14:38 AM6/26/12
to django...@googlegroups.com
The case we're trying to make is -- why do that? Just like Raitucarp said, using an absolute path provides the same end-result as including the hostname and protocol. There's only a few cases where you'd want to do that

Whoops, sorry Raitucarp. I meant Melvyn :)
Reply all
Reply to author
Forward
0 new messages