Code

Opened 3 years ago

Closed 3 years ago

#15765 closed Bug (fixed)

Serving static files in development doesn't work with staticfiles activated

Reported by: Semmel Owned by: nobody
Component: contrib.staticfiles Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a few static files living in STATIC_ROOT that get served correctly when staticfiles is not in INSTALLED_APPS. So using things like http://localhost:8000/static/myfolder/ just work. However when I enable staticfiles things like http://localhost:8000/static/myfolder/ yield a 404 saying "'myfolder\' could not be found". Maybe i'm using staticfiles wrong (i have no idea where site-specific stuff (e.g. favicon, robots, site-global css, etc. should go) but shouldn't the contents in STATIC_ROOT just get served?

settings.py:

STATIC_ROOT = os.path.join(os.path.dirname(__file__), '../static/').replace('\\', '/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

urls.py:

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True)

Attachments (1)

statictest.zip (5.6 KB) - added by Semmel 3 years ago.
A little test project which will reproduce the error.

Download all attachments as: .zip

Change History (7)

comment:1 follow-up: Changed 3 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

Quoting from the docs:

"Many projects will also have static assets that aren’t tied to a particular app; you can give staticfiles additional directories to search via the STATICFILES_DIRS setting ."

The automatic serving during development via staticfiles' runserver command doesn't set show_indexes=True. You don't have to manually add the static pattern to your URLs.

Changed 3 years ago by Semmel

A little test project which will reproduce the error.

comment:2 in reply to: ↑ 1 ; follow-up: Changed 3 years ago by Semmel

Replying to jezdez:

Quoting from the docs:

"Many projects will also have static assets that aren’t tied to a particular app; you can give staticfiles additional directories to search via the STATICFILES_DIRS setting ."

The automatic serving during development via staticfiles' runserver command doesn't set show_indexes=True. You don't have to manually add the static pattern to your URLs.

I've added a little test project to demonstrate the behavior. Just run it and point your browser to http://localhost:8000/static/myfolder/. Of course I could use staticfiles_urlpatterns for serving files in development. For this example I used static and added show_indexes as an easy way to see that the folder really is there (http://localhost:8000/static/ works as expected) but navigating to anything else just doesn't work.

Is there any resource for a standard staticfiles layout? I tried to peek at djangoproject.com but was surprised to see that it doesn't even use staticfiles.

comment:3 in reply to: ↑ 2 Changed 3 years ago by jezdez

Replying to Semmel:

I've added a little test project to demonstrate the behavior. Just run it and point your browser to http://localhost:8000/static/myfolder/. Of course I could use staticfiles_urlpatterns for serving files in development. For this example I used static and added show_indexes as an easy way to see that the folder really is there (http://localhost:8000/static/ works as expected) but navigating to anything else just doesn't work.

As I said, if you use staticfiles, the STATIC_ROOT directory won't be served directly but will use the staticfiles finders instead to find the files linked via STATIC_URL. Feel free to not use staticfiles but the django.core.views.static view if you just want to serve files located in the STATIC_ROOT directory.

Is there any resource for a standard staticfiles layout? I tried to peek at djangoproject.com but was surprised to see that it doesn't even use staticfiles.

Yes, as described in the docs, by default staticfiles will look at the static/ directories of each app and all the directories listed in STATICFILES_DIRS -- similarly to how templates are structured. If you have "common files" that don't fit any of your apps, put them in a separate directory and add that directory to the STATICFILES_DIRS setting.

comment:4 follow-up: Changed 3 years ago by Semmel

  • Easy pickings unset
  • UI/UX unset

Okay - I finally found out what confused me so much. To fully understand my problem you need to know that I tried to update and old project to use staticfiles. This project has no app specific files so there was only one big static folder that got served under /.

When i tried updating it I thought it would be the easiest way to just copy the whole old static directory to STATIC_ROOT. Since surfing to STATIC_URL (I always have show_indexes=True) didn't work I tried the static urlpattern.

And that's when things got strange because sometimes I just couldn't surf the subfolders/files (even though they were shown in the directory listing using show_indexes=True). So long story short - I was just trying to surf to STATIC_URL which didn't work and I have no idea if this is a bug or a feature but I find it really inconvenient that I can't just hack my URLs in development mode (of course using show_indexes=True).

Replying to jezdez:

Replying to Semmel:

Is there any resource for a standard staticfiles layout? I tried to peek at djangoproject.com but was surprised to see that it doesn't even use staticfiles.

Yes, as described in the docs, by default staticfiles will look at the static/ directories of each app and all the directories listed in STATICFILES_DIRS -- similarly to how templates are structured. If you have "common files" that don't fit any of your apps, put them in a separate directory and add that directory to the STATICFILES_DIRS setting.

Okay - got that. But since my original static folder got served under / to allow for serving common files like favicon.ico or robots.txt (and other files which need to be served under /) I have no idea where to put them in this setup. The docs don't say anything about this common case - so where do I put these?

comment:5 in reply to: ↑ 4 Changed 3 years ago by jezdez

  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

Replying to Semmel:

Okay - I finally found out what confused me so much. To fully understand my problem you need to know that I tried to update and old project to use staticfiles. This project has no app specific files so there was only one big static folder that got served under /.

When i tried updating it I thought it would be the easiest way to just copy the whole old static directory to STATIC_ROOT. Since surfing to STATIC_URL (I always have show_indexes=True) didn't work I tried the static urlpattern.

And that's when things got strange because sometimes I just couldn't surf the subfolders/files (even though they were shown in the directory listing using show_indexes=True). So long story short - I was just trying to surf to STATIC_URL which didn't work and I have no idea if this is a bug or a feature but I find it really inconvenient that I can't just hack my URLs in development mode (of course using show_indexes=True).

I got it, you still have staticfiles in INSTALLED_APPS. That will make the runserver automatically serve the files under STATIC_URL, which also means that requests to just STATIC_URL will show an odd error message.

Replying to jezdez:

Replying to Semmel:

Is there any resource for a standard staticfiles layout? I tried to peek at djangoproject.com but was surprised to see that it doesn't even use staticfiles.

Yes, as described in the docs, by default staticfiles will look at the static/ directories of each app and all the directories listed in STATICFILES_DIRS -- similarly to how templates are structured. If you have "common files" that don't fit any of your apps, put them in a separate directory and add that directory to the STATICFILES_DIRS setting.

Okay - got that. But since my original static folder got served under / to allow for serving common files like favicon.ico or robots.txt (and other files which need to be served under /) I have no idea where to put them in this setup. The docs don't say anything about this common case - so where do I put these?

I think you were on the right track and it should be fixed to work like you did. Reopening..

comment:6 Changed 3 years ago by jezdez

  • Resolution set to fixed
  • Status changed from reopened to closed

In [16503]:

Fixed #15765 -- Stopped showing an odd error message when using the staticfiles enabled runserver management command and trying to serve files from STATIC_URL at the same time.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.