django not found static files in the subfolder of static

情到浓时终转凉″ 提交于 2021-02-19 08:01:06

问题


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.


回答1:


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 'images/myapp/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.




回答2:


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



来源:https://stackoverflow.com/questions/24133302/django-not-found-static-files-in-the-subfolder-of-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!