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