Opened 14 years ago

Closed 9 years ago

#16734 closed New feature (fixed)

Set script prefix in django.setup() to allow its usage outside of requests

Reported by: Dougal Sutherland Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: julenx@…, Akos Ladanyi Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no
Pull Requests:5470 merged

Description

The script prefix for django.core.urlresolvers doesn't get set to anything when being called through manage.py, because of course it doesn't know what that value should be. This is a problem if you're rendering views (or otherwise reversing urls) from a manage.py command (as one of my sites does to send emails).

This is solvable by calling set_script_prefix from settings.py, but that feels kind of dirty since it's then about to be rewritten in the WSGI handler.

I don't know what a good solution to this would be. Perhaps it would be nice to be able to set a global default script path somewhere that would then get incorporated into the default values of things like LOGIN_URL.

Maybe just a note in the documentation would be good. It took me a while to figure out, because I haven't been able to find anything else about this online. (I guess that non-/ script paths are uncommon and reversing urls from manage.py is also uncommon, so both together are very uncommon.)

Change History (12)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

Based on code inspection, I confirm that this bug exists.

Possible fix: django.core.management.setup_environ could do something along the lines of:

from django.conf import settings
from django.core.management.base import set_script_prefix
from django.utils.encoding import force_unicode

set_script_prefix(u'/' if settings.FORCE_SCRIPT_NAME is None else force_unicode(settings.FORCE_SCRIPT_NAME))

comment:2 by julen, 11 years ago

Cc: julenx@… added

comment:3 by Akos Ladanyi, 11 years ago

Cc: Akos Ladanyi added

comment:4 by Akos Ladanyi, 11 years ago

If it is not possible to figure out the value of script_prefix in all cases (like in manage.py) why not just store its value in a settings variable? Then reverse could just prepend this value to all paths deduced from urlconf.

comment:5 by julen, 10 years ago

Would it make sense to call set_script_prefix() in ManagementUtility's execute() method, once settings have been configured?

comment:6 by Tim Graham, 10 years ago

django.setup() seems to be a natural place for it.

comment:7 by julen, 10 years ago

I thought so initially, but this issue is limited to management commands, that's why I wonder about ManagementUtility.execute(). Once the right location for this is confirmed, the fix should be trivial.

comment:8 by Tim Graham, 10 years ago

I imagine it would also affect standalone scripts that invoke django.setup() but don't use ManagementUtility.

comment:9 by Claude Paroz, 9 years ago

Has patch: set
Needs documentation: set
Version: 1.3master

comment:10 by Claude Paroz, 9 years ago

Needs documentation: unset

comment:11 by Tim Graham, 9 years ago

Summary: urlresolvers doesn't get a script prefix from manage.pySet script prefix in django.setup() to allow its usage outside of requests
Triage Stage: AcceptedReady for checkin
Type: BugNew feature

comment:12 by Claude Paroz <claude@…>, 9 years ago

Resolution: fixed
Status: newclosed

In 7d81ee6e:

Fixed #16734 -- Set script prefix even outside of requests

Thanks Tim Graham for the review.

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