1

I am trying to load static files in my development environment in django. My settings look like the following:

STATIC_ROOT = '/MyProject/myapp/static/'
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
)

my site urls.py looks like:

from django.conf import settings
import views

from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = patterns('',

) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

My header-include.html looks like:

<link href="{% static 'css/mycss.css' %}" media="screen" rel="stylesheet" type="text/css">

My css director is located at:

mysite > static > css

I get the error:

localhost/static/css/mycss.css 404 (NOT FOUND)

What am I doing wrong???

Atma
  • 29,141
  • 56
  • 198
  • 299
  • Dont need staticfiles finder nor static root – wonderwhy Aug 26 '14 at 23:39
  • Statics urls dont have to be mapped – wonderwhy Aug 26 '14 at 23:40
  • You need to add the staticfiles contrib app to your apps list. – wonderwhy Aug 26 '14 at 23:40
  • Create a folder called static in your project folder and it should just work – wonderwhy Aug 26 '14 at 23:41
  • @wonderwhy I do have app installed. I added more detail – Atma Aug 26 '14 at 23:46
  • @wonderwhy I deleted the url mapping and it still doesn't find the css. Do you think the css folder is in the right place above? – Atma Aug 26 '14 at 23:47
  • Dont need to map static requests. They are mapped by the app. See my answer to check if you have it all and then check if the file is called that way. If it still gived 404, tell me – wonderwhy Aug 26 '14 at 23:47
  • @wonderwhy I removed the url (+ static(settings.STATIC_URL ...). I also created another static folder in both project_root > static and project_root > mysite_folder > static. I still get the 404. Thanks for all of the help so far. – Atma Aug 27 '14 at 00:02

3 Answers3

8

Dont need files finder nor static root. Add the djando.contrib.staticfiles app to your apps. Delete the url mapping and create the folder in your project root. It should just work with the link you generate.

So code:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

Or place the folder inside the app. But if you do so, you have to call the static url as:

{{ STATIC_URL }}

or just use /static/. Then after that django template call, add the path to file.

If you want to use it inside the app, use the tag you are using. That's for specific files uses, like a custom stylesheet, or js file, but for general styles better place it where I tell you otherwise u'll gotta copy it in each app if you you wanna use it.

wonderwhy
  • 393
  • 2
  • 19
  • I removed the line: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) and it still did not work – Atma Aug 27 '14 at 00:40
  • The key for me was the STATICFILES_DIRS line, I needed to include it. – Kyle Burkett Jul 14 '20 at 01:08
  • Moving static folder which is part of the project directory to one of the app worked also moving back to the original project folder worked too with the above setting enabled.. I believe django refreshes in this way. As STATICFILES_DIRS settings is clearly meant for static folders outside of app – ivardu Dec 14 '20 at 16:08
1

In django2 you just need to create static and templates folder for individual app by default template render and static files look for individual app for static and templates folder, in case even after creating folder and copying files doesn't work, try to stop and run the server again, it does the fix :)

  • 1
    I have a static folder in a sub-app, which wasn't recognized by Django in development. Restarting the server actually fixed that. – webtweakers Jun 20 '19 at 19:50
  • @webtweakers Thank you. That comment saved me from a hour of beating my head against a wall after creating my new app. – Michael Romrell Apr 18 '20 at 03:27
  • @MichaelRomrell, I did mention to stop and run the dev server again, no problem as long you found the solution, thanks for the comment webtweakers – Murugaraju Perumalla Dec 11 '20 at 18:44
0

I needed to move my static folder to an app. The django docs specify that django will look for a static folder in each of the apps. I guess the site root folder doesn't count as those and so it wasn't found.

Thanks to this post: Django Static Files Development

Community
  • 1
  • 1
Atma
  • 29,141
  • 56
  • 198
  • 299
  • Yeah that is true. But you don't need to do so if you don't want to. You just gotta add what I have in my answer to your code – wonderwhy Aug 27 '14 at 11:19