Opened 16 years ago

Last modified 7 years ago

#10944 assigned New feature

Site app should be able to make absolute URLs.

Reported by: Jeremy Dunck Owned by: Christopher Medrela
Component: contrib.sites Version: 1.0
Severity: Normal Keywords:
Cc: krzysiumed@…, André Cruz Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I wish Site instances could make (real) absolute URLs (e.g. http.../path/) based on a given relative path like /path/. And a template tag to make it nicer from templates, too:
{% site_url ... %} or similar.

Can I have a pony? I'll write the patch if I can have a pony.

Attachments (3)

site_url.diff (2.1 KB ) - added by laurentluce 14 years ago.
get_url Site model method and site_url template tage
site_url.2.diff (4.8 KB ) - added by laurentluce 14 years ago.
add get_url() to RequestSite. Add support for protocol to get_url() and template tag site_url. Update docs.
10944_v3.diff (7.6 KB ) - added by Christopher Medrela 12 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 by daniellindsley, 15 years ago

milestone: 1.21.3

Deadline for features in 1.2 is past. Deferring to 1.3.

comment:2 by Jannis Leidel, 14 years ago

Triage Stage: Design decision neededAccepted

comment:3 by anonymous, 14 years ago

Owner: changed from nobody to laurentluce

comment:4 by anonymous, 14 years ago

Owner: changed from laurentluce to anonymous
Status: newassigned

comment:5 by laurentluce, 14 years ago

Owner: changed from anonymous to laurentluce
Status: assignednew

comment:6 by laurentluce, 14 years ago

Status: newassigned

by laurentluce, 14 years ago

Attachment: site_url.diff added

get_url Site model method and site_url template tage

comment:7 by laurentluce, 14 years ago

Has patch: set
Needs documentation: set
  • add method get_url to Site model to return an absolute url based on a relative path passed as an argument.
  • add site_url template tag doing the same

comment:8 by Gabriel Hurley, 14 years ago

Patch needs improvement: set

Two thoughts:

  1. This method should be available on both Site and RequestSite. Using the new django.contrib.sites.models.get_current_site function (which returns either a Site or RequestSite instance) you could then construct full URLs without being tied to the sites framework.
  1. It seems like constructing the full URL is good, but personally I'd like to have it add the protocol (http:// or https://) as well. That bit is a little bit trickier and may not be within the scope of the Site object to know what the right protocol is. It just seems like a halfway-there solution to get a full URL but have to manually type the "http://".

And, as you already noted, the patch would need docs before it's done.

comment:9 by Chris Beaven, 14 years ago

What's actually the good in the {% site_url %} tag? It seems like it should be able to take other sites, too (if you only have one site, why bother using this over the standard request.build_absolute_uri()?)

Bikeshedding, it could potentially just be a filter: {% url index as home_url %}{{ client.site|site_url:home_url }}
But in that case, you'd probably need a {% get_current_site as site %} tag too...

Anyway, just a bit of late night rambling.

by laurentluce, 14 years ago

Attachment: site_url.2.diff added

add get_url() to RequestSite. Add support for protocol to get_url() and template tag site_url. Update docs.

comment:10 by laurentluce, 14 years ago

Needs documentation: unset
Patch needs improvement: unset

comment:11 by James Bennett, 14 years ago

milestone: 1.3

1.3 is feature-frozen now.

comment:12 by Gabriel Hurley, 14 years ago

Component: Contrib appscontrib.sites

comment:13 by Julien Phalip, 14 years ago

Severity: Normal
Type: New feature

See also #13559 which suggests adding a context processor for this.

comment:14 by laurentluce, 13 years ago

Easy pickings: unset
Owner: laurentluce removed
Status: assignednew
UI/UX: unset

comment:15 by Tomek Paczkowski, 13 years ago

site_url tag is a bit useless, as it take path as string. IMHO it should work like the url tag taking names and parameters.

by Christopher Medrela, 12 years ago

Attachment: 10944_v3.diff added

comment:16 by Christopher Medrela, 12 years ago

Cc: krzysiumed@… added
Owner: set to Christopher Medrela

I added my draft where site_url is similar to url tag (actually it calls the url tag function and create instance of URLNode to avoid redundancy). It includes tests and docs but my english is poor. An example of using my site_url:

  {% site_url site 'myapp.views.viewname' %} ==> 'http://example.com/path/to/view'
  {% site_url site using https 'myapp.views.viewname' %} ==> 'https://example.com/path/to/view'

comment:17 by Christopher Medrela, 12 years ago

Status: newassigned

comment:18 by Tim Graham, 11 years ago

Patch needs improvement: set

Patch no longer applies cleanly.

in reply to:  9 comment:19 by Markus Bertheau, 10 years ago

Replying to SmileyChris:

What's actually the good in the {% site_url %} tag? It seems like it should be able to take other sites, too (if you only have one site, why bother using this over the standard request.build_absolute_uri()?)

I often find myself in a situation where I need an absolute URL but cannot use request.build_absolute_uri() because I don't have a request: cron jobs, celery tasks or api modules that I don't want to couple to the request. Afaics Django currently doesn't have a place to generate absolute URLs without a request, so everyone is hacking their own way. Site is a possible place to do it, but it'd have to learn about the scheme and port to be complete. RequestSite could take these from new settings, maybe: SITE_SCHEME, SITE_PORT.

comment:20 by André Cruz, 7 years ago

Cc: André Cruz added

Seems like this would be a pretty useful feature to have, and one that I need.

Is any one of those "hacks" blessed by the Django community? I'm doing this:

def handcraft_absolute_url(path):
    domain = Site.objects.get_current().domain
    scheme = 'https' if SESSION_COOKIE_SECURE else 'http'
    return '{scheme}://{domain}{path}'.format(domain=domain, path=path, scheme=scheme)
Note: See TracTickets for help on using tickets.
Back to Top