user266003
user266003

Reputation:

Django can't find static files for an app

I have static files placed in my_site/my_app/static/my_app/js and my_site/my_app/static/my_app/css. For some reason, the code below doesn't produce any output which means it can't find the static files:

#my_app/templates/my_app/base.html

{% load staticfiles %}

Here is setting.py

STATIC_URL = '/static/'


STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'my_app', 'static', 'my_app',).replace('\\','/'),
) 

STATIC_ROOT = ''

Why is that?

Upvotes: 0

Views: 2213

Answers (3)

d-coder
d-coder

Reputation: 13963

I had faced the same issue which got solved after the following changes.

In HTML pages:

{% load static %}  ## loads the static folder and images inside it. 
<div id='button-holder'><img src="{% static "glass.png" %}" alt="Hi!" /></div> ## for images 
                              src="{% static 'my_app/js/app.js' %} ## for scripts.

In urls.py

urlpatterns = patterns('',...
........
 )+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

After this run command as @Daniel Roseman have mentioned python manage.py collectstatic. The findstatic command can help show you which files are found. Example

python manage.py findstatic css/base.css admin/js/core.js

You can find help here regarding it.

Upvotes: 0

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

Add django.contrib.staticfiles to INSTALLED_APPS in your settings.py.

Remove STATICFILES_FINDERS, STATICFILES_DIRS, STATIC_ROOT from your settings.py.

change your base.html to something like this:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="fa">
    <head>
        <script type="text/javascript" src="{% static 'my_app/js/app.js' %}"></script>
        <title>{{ title }}</title>
    </head>
    <body>
        {% block content %}
        {% endblock %}
    </body>
</html>

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599600

You are supposed to run manage.py collectstatic to copy your app-level static files to the central static directory.

Upvotes: -1

Related Questions