Not "entirely separate". These are projects sharing much of the
codebase, just having different SITE_ID in settings.
Anyway there's still my first use-case of constructing URLs from
standalone scripts.
> We pass around request objects all over the place in order to get this
> functionality. request.build_absolute_uri is great, but it does
> require that request object and passing it around - which sucks.
request.build_absolute_uri works for you because you don't have those
use-cases. This is why we see this way differently: you think that
having a request universally available makes things more convenient
while in my view it doesn't fix URL construction at all[*] and just adds
a bit to the mess in thread-locals. And I kind of disagree that my
use-cases very exotic.
[*] URL construction is really broken right now because I am forced to
hard-code 'http://' or 'https://' in my script that sends mail by cron.
My point is that we better fix these annoyances than leave this way of
doing things altogether.
> I never use the Site module
This is by itself is not the reason why it might be annoying. Django
actually recommends its usage in documentation and if you don't follow
it then, why, some level of annoyance is expected.
>, and in my experience having to
> change the "FRONTEND_URL" of your app every time you push to a
> different environment is tedious and a frequent source of subtle
> problems.
Indeed. This is why there's Site model. It's better than a setting
because it's per-database, not per-installation.
I agree that having to maintain a record in Site is also not ideal. But
it can be made less error-prone:
- syncdb could interactively ask for scheme/host/port instead of using
"example.com" (that would fix all *my* problems with SIte)
- correct set of sites can live in initial_data fixture
> Moreover, the request information in your current request
> _should_ always be correct.
Agreed. But it's not the whole point I'm arguing.
> That said, it sounds like there are a number of special cases where it
> would be useful to override these settings. So maybe the best corse of
> action is to try to use the configured Site information and fall back
> on "RequestSite", which uses information from the currently active
> request.
This might work, I think. Anyway I can't bash this idea with anything
from the top of my head :-). I'll think of it a bit more.
Aha! I was pretty confused by this thread, since I didn't remember
writing the above code. It turns out that's the problem with making
proposals like this on a wiki...
http://code.djangoproject.com/wiki/ReplacingGetAbsoluteUrl?action=diff&version=10
My original proposal can still be seen here:
http://code.google.com/p/django-urls/source/browse/trunk/django_urls/base.py
It's basically this:
def get_url(self):
if hasattr(self.get_url_path, 'dont_recurse'):
raise NotImplemented
try:
path = self.get_url_path()
except NotImplemented:
raise
prefix = getattr(settings, 'DEFAULT_URL_PREFIX', 'http://
localhost')
return prefix + path
get_url.dont_recurse = True
So for the common case it invents a new DEFAULT_URL_PREFIX setting
which can be used to ensure get_url uses the correct domain. If you
want to use a threadlocal request for this instead that's fine - just
define your own get_url() method that does that.
Cheers,
Simon