← All challenges
harddjango/django · v3.1

Add support for SCRIPT_NAME in STATIC_URL and MEDIA_URL

Try this bug with AI

Fix it in the original repo with an AI agent and get feedback on how you worked.

2 free rounds each week

Round mode
  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to310 XP
Sign up or sign in to start

Read the sample report

From a simulated Django round

You changed code before reproducing the bug.

Run the failing test before the first edit, so you know the failure you are fixing and can tell when it is gone.

Problem statement

Description

	(last modified by Rostyslav Bryzgunov)

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.

The environment starts at commit 580e644f24f1 (django/django 3.1), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.