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)
Change History (23)
comment:1 by , 15 years ago
milestone: | 1.2 → 1.3 |
---|
comment:2 by , 14 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:3 by , 14 years ago
Owner: | changed from | to
---|
comment:4 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:6 by , 14 years ago
Status: | new → assigned |
---|
by , 14 years ago
Attachment: | site_url.diff added |
---|
get_url Site model method and site_url template tage
comment:7 by , 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 , 14 years ago
Patch needs improvement: | set |
---|
Two thoughts:
- 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.
- 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.
follow-up: 19 comment:9 by , 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 , 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 , 14 years ago
Needs documentation: | unset |
---|---|
Patch needs improvement: | unset |
comment:12 by , 14 years ago
Component: | Contrib apps → contrib.sites |
---|
comment:13 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
See also #13559 which suggests adding a context processor for this.
comment:14 by , 13 years ago
Easy pickings: | unset |
---|---|
Owner: | removed |
Status: | assigned → new |
UI/UX: | unset |
comment:15 by , 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 , 12 years ago
Attachment: | 10944_v3.diff added |
---|
comment:16 by , 12 years ago
Cc: | added |
---|---|
Owner: | set to |
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 , 12 years ago
Status: | new → assigned |
---|
comment:19 by , 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 standardrequest.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 , 7 years ago
Cc: | 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)
Deadline for features in 1.2 is past. Deferring to 1.3.