Opened 15 years ago

Last modified 6 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 13 years ago.
get_url Site model method and site_url template tage
site_url.2.diff (4.8 KB) - added by laurentluce 13 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 11 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 Changed 14 years ago by daniellindsley

milestone: 1.21.3

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

comment:2 Changed 13 years ago by Jannis Leidel

Triage Stage: Design decision neededAccepted

comment:3 Changed 13 years ago by anonymous

Owner: changed from nobody to laurentluce

comment:4 Changed 13 years ago by anonymous

Owner: changed from laurentluce to anonymous
Status: newassigned

comment:5 Changed 13 years ago by laurentluce

Owner: changed from anonymous to laurentluce
Status: assignednew

comment:6 Changed 13 years ago by laurentluce

Status: newassigned

Changed 13 years ago by laurentluce

Attachment: site_url.diff added

get_url Site model method and site_url template tage

comment:7 Changed 13 years ago by laurentluce

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 Changed 13 years ago by Gabriel Hurley

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 Changed 13 years ago by Chris Beaven

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.

Changed 13 years ago by laurentluce

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 Changed 13 years ago by laurentluce

Needs documentation: unset
Patch needs improvement: unset

comment:11 Changed 13 years ago by James Bennett

milestone: 1.3

1.3 is feature-frozen now.

comment:12 Changed 13 years ago by Gabriel Hurley

Component: Contrib appscontrib.sites

comment:13 Changed 13 years ago by Julien Phalip

Severity: Normal
Type: New feature

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

comment:14 Changed 12 years ago by laurentluce

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

comment:15 Changed 12 years ago by Tomek Paczkowski

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.

Changed 11 years ago by Christopher Medrela

Attachment: 10944_v3.diff added

comment:16 Changed 11 years ago by Christopher Medrela

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 Changed 11 years ago by Christopher Medrela

Status: newassigned

comment:18 Changed 9 years ago by Tim Graham

Patch needs improvement: set

Patch no longer applies cleanly.

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

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 Changed 6 years ago by André Cruz

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