Proposal: Constructing urls outside the request cycle

111 views
Skip to first unread message

Sarah Boyce

unread,
Apr 6, 2023, 3:11:43 AM4/6/23
to Django developers (Contributions to Django itself)
Hello 👋 

This is a summary of some previous discussions which need opening up to the group 👍

There is a need to have some kind of URL outside the request cycle (such as sending emails which need to create a link).
For constructing a correct MEDIA_URL / STATIC_URL we wanted to use the script prefix to allow for more dynamic changes (ie allow a user to move to a subpath without having to change multiple variables). In the request cycle you can use get_script_prefix() for this but outside the request cycle this isn't currently very achievable (see related tickets #34028 #16734).

There is a current proposal from @Florian Apolloner:
"I am really starting to lean towards introducing a setting (yes!) called BASE_URL that can be set to https://mysite.com/subpath. This would allow for a) generating URLs outside a request context and b) allow us to mostly ignore script prefix. We could also use a relative STATIC_URL and append it to BASE_URL. This way there would be just one setting that requires changing and many projects hopefully would make that configurable via env vars etc."

New settings are a little controversial, so what do you think about this approach? Are there any other ideas or concerns?

Adam Johnson

unread,
Apr 16, 2023, 4:21:20 AM4/16/23
to django-d...@googlegroups.com
A setting isn't so mad. Many Django projects I've seen use a similar custom setting for constructing URLs outside of requests.

One question though, how will we support projects that are served at multiple domains? Would Django only support the "main" site through BASE_URL and require you to perform URL construction for other domains as required?

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/df82f370-e96a-498c-ac08-89d34d5cadd5n%40googlegroups.com.

Florian Apolloner

unread,
Apr 17, 2023, 3:25:42 AM4/17/23
to Django developers (Contributions to Django itself)
On Sunday, April 16, 2023 at 10:21:20 AM UTC+2 Adam Johnson wrote:
One question though, how will we support projects that are served at multiple domains? Would Django only support the "main" site through BASE_URL and require you to perform URL construction for other domains as required?

As usual it depends (tm) :D As long as `reverse` keeps generating URLs without a host then we'd only use BASE_URL as a fallback in certain cases where no request is available. In the longrun I'd love to support something along the lines of https://werkzeug.palletsprojects.com/en/2.2.x/routing/#werkzeug.routing.Subdomain and then `reverse` would probably be able to generate the proper URLs. But yes, there are certainly many things to think about.
Reply all
Reply to author
Forward
0 new messages