Opened 6 years ago

Last modified 6 months ago

#10944 assigned New feature

Site app should be able to make absolute URLs.

Reported by: jdunck Owned by: krzysiumed
Component: contrib.sites Version: 1.0
Severity: Normal Keywords:
Cc: krzysiumed@… 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 5 years ago.
get_url Site model method and site_url template tage
site_url.2.diff (4.8 KB) - added by laurentluce 5 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 krzysiumed 3 years ago.

Download all attachments as: .zip

Change History (22)

comment:1 Changed 5 years ago by daniellindsley

  • milestone changed from 1.2 to 1.3

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

comment:2 Changed 5 years ago by jezdez

  • Triage Stage changed from Design decision needed to Accepted

comment:3 Changed 5 years ago by anonymous

  • Owner changed from nobody to laurentluce

comment:4 Changed 5 years ago by anonymous

  • Owner changed from laurentluce to anonymous
  • Status changed from new to assigned

comment:5 Changed 5 years ago by laurentluce

  • Owner changed from anonymous to laurentluce
  • Status changed from assigned to new

comment:6 Changed 5 years ago by laurentluce

  • Status changed from new to assigned

Changed 5 years ago by laurentluce

get_url Site model method and site_url template tage

comment:7 Changed 5 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 5 years ago by gabrielhurley

  • 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 follow-up: Changed 5 years ago by 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()?)

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

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

comment:10 Changed 5 years ago by laurentluce

  • Needs documentation unset
  • Patch needs improvement unset

comment:11 Changed 5 years ago by ubernostrum

  • milestone 1.3 deleted

1.3 is feature-frozen now.

comment:12 Changed 4 years ago by gabrielhurley

  • Component changed from Contrib apps to contrib.sites

comment:13 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to New feature

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

comment:14 Changed 4 years ago by laurentluce

  • Easy pickings unset
  • Owner laurentluce deleted
  • Status changed from assigned to new
  • UI/UX unset

comment:15 Changed 3 years ago by oinopion

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 3 years ago by krzysiumed

comment:16 Changed 3 years ago by krzysiumed

  • Cc krzysiumed@… added
  • Owner set to krzysiumed

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 3 years ago by krzysiumed

  • Status changed from new to assigned

comment:18 Changed 14 months ago by timo

  • Patch needs improvement set

Patch no longer applies cleanly.

comment:19 in reply to: ↑ 9 Changed 6 months ago by mbertheau

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.

Note: See TracTickets for help on using tickets.
Back to Top