#25598 closed New feature (fixed)
Add support for SCRIPT_NAME in STATIC_URL and MEDIA_URL
| Reported by: | Dheerendra Rathor | Owned by: | Oleg Kainov |
|---|---|---|---|
| Component: | contrib.staticfiles | Version: | dev |
| Severity: | Normal | Keywords: | script_name, static_url, media_url |
| Cc: | kottenator@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
By default, {% static '...' %} tag just appends STATIC_URL in the path. When running on sub-path, using SCRIPT_NAME WSGI param, it results in incorrect static URL - it doesn't prepend SCRIPT_NAME prefix.
This problem can be solved with prepending SCRIPT_NAME to STATIC_URL in settings.py but that doesn't work when SCRIPT_NAME is a dynamic value.
This can be easily added into default Django static tag and django.contrib.staticfiles tag as following:
def render(self, context):
url = self.url(context)
# Updating url here with request.META['SCRIPT_NAME']
if self.varname is None:
return url
context[self.varname] = url
return ''
On more research I found that FileSystemStorage and StaticFilesStorage ignores SCRIPT_NAME as well.
We might have to do a lot of changes but I think it's worth the efforts.
Change History (22)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
This is not a patch. This is just an idea I got about the patch for {% static %} only. The patch will (probably) involve FileSystemStorage and StaticFileSystemStorage classes.
The main idea behind this feature was that Django will auto detect script_name header and use that accordingly for creating static and media urls. This will reduce human efforts for setting up sites in future. This patch will also take time to develop so it can be added in Django2.0 timeline.
comment:3 by , 10 years ago
What I meant was that I don't think Django should automatically use SCRIPT_NAME in generating those URLs. If you're running your site on a subpath, then you should set your STATIC_URL to 'http://example.com/subpath/static/' or whatever. However, you might not even be hosting static and uploaded files on the same domain as your site (in fact, for user-uploaded files, you shouldn't do that for security reasons) in which case SCRIPT_URL is irrelevant in constructing the static/media URLs.
How would the change make it easier to setup sites?
comment:4 by , 10 years ago
I think that the idea basically makes sense. Ideally, a Django instance shouldn't need to know at which subpath it is being deployed, as this can be considered as purely sysadmin stuff. It would be a good separation of concerns. For example, the Web administrator may change the WSGIScriptAlias from /foo to /bar and the application should continue working. Of course, this only applies when *_URL settings are not full URIs.
In practice, it's very likely that many running instances are adapting their *_URL settings to include the base script path, hence the behavior change would be backwards incompatible. The question is whether the change is worth the incompatibility.
comment:5 by , 10 years ago
| Component: | Uncategorized → contrib.staticfiles |
|---|
I see. I guess the idea would be to use get_script_prefix() like reverse() does as I don't think we have access to request everywhere we need it. It seems like some public APIs like get_static_url() and get_media_url() would replace accessing the settings directly whenever building URLs. For backwards compatibility, possibly these functions could try to detect if the setting is already prefixed appropriately.
Removing the prefix from the settings, however, means that the URLs are no longer correct when generated outside of a request/response cycle though (#16734). I'm not sure if it might create any practical problems, but we might think about addressing that issue first.
comment:6 by , 10 years ago
| Summary: | support for SCRIPT_NAME for static tag and FileField.url → Add support for SCRIPT_NAME in STATIC_URL and MEDIA_URL |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:7 by , 9 years ago
| Cc: | added |
|---|
comment:8 by , 9 years ago
I'm here at DjangoCon US 2016 will try to create a patch for this ticket ;)
comment:9 by , 9 years ago
| Description: | modified (diff) |
|---|
comment:10 by , 9 years ago
| Description: | modified (diff) |
|---|
comment:11 by , 9 years ago
| Has patch: | set |
|---|
Pull-request created: https://github.com/django/django/pull/7000
comment:12 by , 9 years ago
| Patch needs improvement: | set |
|---|
At least documentation and additional tests look like they are required.
comment:13 by , 9 years ago
Absolutely agree with your remarks, Tim. I'll add tests. Could you point to docs that need to be updated?
comment:14 by , 7 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
I would like to take this ticket on and have a new PR for it: https://github.com/django/django/pull/10724
comment:15 by , 6 years ago
I tried to revive activities around this ticket, so I created (yet another) PR based on previous work https://github.com/django/django/pull/11564
comment:16 by , 6 years ago
| Patch needs improvement: | unset |
|---|
comment:17 by , 6 years ago
| Owner: | changed from to |
|---|
comment:18 by , 6 years ago
| Patch needs improvement: | set |
|---|
comment:19 by , 6 years ago
| Patch needs improvement: | unset |
|---|
comment:20 by , 6 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
This change doesn't seem correct to me (for one, it seems like it could break existing sites). Why not include the appropriate prefix in your
STATIC_URLandMEDIA_URLsettings?