#34028: Django 'static' template tag fails to generate URLs with SCRIPT_NAME 
prefix
---------------------------------+------------------------------------
     Reporter:  Stewart Adam     |                    Owner:  nobody
         Type:  Bug              |                   Status:  new
    Component:  Template system  |                  Version:  4.0
     Severity:  Normal           |               Resolution:
     Keywords:                   |             Triage Stage:  Accepted
    Has patch:  0                |      Needs documentation:  0
  Needs tests:  0                |  Patch needs improvement:  0
Easy pickings:  0                |                    UI/UX:  0
---------------------------------+------------------------------------
Changes (by David Sanders):

 * cc: David Sanders (added)


Comment:

 I've been investigating `SCRIPT_NAME` / `FORCE_SCRIPT_NAME` and found some
 interesting quirks when used with `runserver` which may be affecting
 testing these issues (at least it was affecting me when investigating
 #34185

 I'm not entirely sure why yet but I'll post the following reproducible
 issue (at least for me) to show that prefixing `STATIC_URL` works
 intermittently due to a race condition with runserver and the multiple
 processes setup with autoreload:


 In `django/conf/__init__.py` I added a line to print the result of
 prefixing `STATIC_URL`:
 {{{
     @staticmethod
     def _add_script_prefix(value):
         """
         Add SCRIPT_NAME prefix to relative paths.

         Useful when the app is being served at a subpath and manually
 prefixing
         subpath to STATIC_URL and MEDIA_URL in settings is inconvenient.
         """
         # Don't apply prefix to absolute paths and URLs.
         if value.startswith(("http://";, "https://";, "/")):
             return value
         from django.urls import get_script_prefix

         val = "%s%s" % (get_script_prefix(), value)
         print("*********** PREFIXING ***********: " + val)
         return val
 }}}

 The result when `runserver` is run with autoreload on then off:

 {{{
 sample-project % dj runserver
 Watching for file changes with StatReloader
 Performing system checks...

 *********** PREFIXING ***********: /static/
 System check identified no issues (0 silenced).
 November 29, 2022 - 13:22:27
 Django version 4.2.dev20221129074011, using settings
 'django_sample.settings'
 Starting development server at http://127.0.0.1:8000/
 Quit the server with CONTROL-C.
 ^C
 django-sample % dj runserver --noreload
 Performing system checks...

 *********** PREFIXING ***********: /django/static/
 System check identified no issues (0 silenced).
 November 29, 2022 - 13:22:40
 Django version 4.2.dev20221129074011, using settings
 'django_sample.settings'
 Starting development server at http://127.0.0.1:8000/
 Quit the server with CONTROL-C.
 ^C
 sample-project %
 }}}

 Now the interesting thing is that I can get the same results without
 specifying `--noreload` by just adding a line to `print()` in `setup()` !
 :)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34028#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070184c391683d-e8e38f92-e76d-4904-a796-75f4c538da4a-000000%40eu-central-1.amazonses.com.

Reply via email to