django not found static files in the subfolder of static

Question:

django 1.6.5

My static files is under subfolder of static.

djangoprj
    djangoprj
        settings.py
        urls.py
        wsgi.py
        __init__.py
    static
        myapp
            mystaticfiles
            ...
    apps
        myapp
            ...
            templates
                *.html
            ...

In my templates file, I link static file as full path.

<img src="/static/myapp/images/my.png" />

But those file are not found. I got 404 error.

When change settings.py as

STATIC='/static/myapp/'

I can get those static files. But I am not want to do that. Why dose it not wok, when STATIC='/static/'? Is there some easy way to solve this problem instead of doing command manaully, such as ‘collectstatic’? I have put my static file in static folder, and I have used full path in my templates file. Why does it not work? Thanks in advance.

Asked By: Nick Dong

||

Answers:

Now that you switch the static file folder to '/static/myapp/'

You should use <img src="/static/images/my.png" /> in your tag.

Generally, you should use {% static %} template tag to do this, like below (assume STATIC='/static/').

{% load staticfiles %}

<img src="{% static 'myapp/images/my.png' %}" />

to learn more, see the tutorial, https://docs.djangoproject.com/en/1.6/intro/tutorial06/

I suggest you to read all the tutorial (part1 – part6) in https://docs.djangoproject.com/en/1.6/

So that you can know deep enough for the basis.

Answered By: Alfred Huang

You can found some help in these answers also

  1. static files problem
  2. STATIC_URL Not Working

In my configuration I have a directory structure where static files are inside my app like this

djangoprj
    djangoprj
        settings.py
        urls.py
        wsgi.py
        __init__.py
    apps
        myapp
            ...
            templates
                *.html
            static   
                myapp
                   mystaticfiles
            ...
            ...

In settings

INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    ...
)

STATIC_URL = '/static/'

and in templates I use ‘static’ tags

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'posts/style.css' %}" />

I hope this can help you

Answered By: Salvatore Avanzo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.