簡體   English   中英

Django找不到應用程式的靜態檔案

[英]Django can't find static files for an app

我將靜態文件放在my_site/my_app/static/my_app/jsmy_site/my_app/static/my_app/css 由於某些原因,下面的代碼不會產生任何輸出,這意味着它找不到靜態文件:

#my_app/templates/my_app/base.html

{% load staticfiles %}

這是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 = ''

這是為什么?

django.contrib.staticfiles添加到settings.py INSTALLED_APPS中。

刪除STATICFILES_FINDERSSTATICFILES_DIRSSTATIC_ROOTsettings.py

將您的base.html更改為以下內容:

{% 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>

經過以下更改,我也遇到了同樣的問題。

在HTML頁面中:

{% 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.

在urls.py中

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

在@Daniel Roseman這樣的運行命令之后,提到了python manage.py collectstatic findstatic命令可以幫助您顯示找到了哪些文件。

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

您可以在此處找到相關幫助。

您應該運行manage.py collectstatic將應用程序級靜態文件復制到中央靜態目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM