简体   繁体   English

在 Django 中找不到静态文件错误

[英]static files not found error in django

I know that this problem has already been asked several times here.我知道这个问题已经在这里被问过好几次了。 I have searched and read a number of answers but no help.我已经搜索并阅读了许多答案,但没有帮助。 I guess, I am missing something very basic.我想,我错过了一些非常基本的东西。

In my settings.py, I have:在我的 settings.py 中,我有:

STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
# STATICFILES_DIRS = [join(APPS_DIR, 'static')]

MEDIA_ROOT = join(APPS_DIR, 'media')
MEDIA_URL = "/media/"

In my config/urls.py, I have:在我的 config/urls.py 中,我有:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I have a file located at /static/core/js/jquery_countdown/jquery.countdown.min.js which I am trying to load in template as below:我有一个位于/static/core/js/jquery_countdown/jquery.countdown.min.js的文件,我试图将其加载到模板中,如下所示:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript" src="{% static 'core/js/jquery_countdown/jquery.countdown.min.js' %}"> </script>

The top of the same template looks like同一模板的顶部看起来像

{% extends "contest/base.html" %}
{% load static %}

This results in following server error:这导致以下服务器错误:

[23/Mar/2018 10:12:08] "GET /static/core/js/jquery_countdown/jquery.countdown.min.js HTTP/1.1" 404 1858

What am I missing?我错过了什么?

You are searching for static files in app directories only but have your file in global static files. 您只在app目录中搜索静态文件,但将文件保存在全局静态文件中。 You should use the commented STATICFILES_DIRS setting to specify all places to search for static files. 您应该使用注释的STATICFILES_DIRS设置来指定搜索静态文件的所有位置。

Create folder static_files in your app directory. 在app目录中创建文件夹static_files And place all your static files inside it. 并将所有静态文件放在其中。 Then use the following settings 然后使用以下设置

STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
STATICFILES_DIRS = [join(APPS_DIR, 'static_files')]

If it does not solve your issue, then run the command python manage.py collectstatic . 如果它没有解决您的问题,那么运行命令python manage.py collectstatic It will copy all the static files (Not only from your app but also from django admin, third party apps etc) to the STATIC_ROOT folder. 它会将所有静态文件(不仅来自您的应用程序,还来自django admin,第三方应用程序等)复制到STATIC_ROOT文件夹。

Details 细节

For local serving, django server will look in to the STATICFILES_DIRS . 对于本地服务,django服务器将查看STATICFILES_DIRS So you dont need to run the python manage.py collectstatic command. 所以你不需要运行python manage.py collectstatic命令。 All the external apps may have STATICFILES_DIRS where they place their static files. 所有外部应用程序都可能具有静态文件的STATICFILES_DIRS。 For production server you need to collect all these static files scattered around all your apps in to a single place. 对于生产服务器,您需要将分散在所有应用程序周围的所有静态文件收集到一个位置。 Thats what python manage.py collectstatic command will do. 多数民众赞成python manage.py collectstatic命令会做什么。 It will copy all the static files in to STATIC_ROOT directory 它会将所有静态文件复制到STATIC_ROOT目录中

I found that this worked for me.我发现这对我有用。 (Development) (发展)
(I chose to name it "zStatic" to keep it at the bottom of the root (project folder), so that it isnt amidst all my apps. Easier to locate.) (我选择将其命名为“zStatic”以将其保留在根目录(项目文件夹)的底部,这样它就不会出现在我所有的应用程序中。更容易找到。)

Settings.py设置.py

STATIC_URL = 'zStatic/'  
STATICFILES_DIRS = (  
BASE_DIR/'zStatic',  
)

INSTALLED_APPS = 
[
...
'django.contrib.staticfiles',
...
]

base.html ( base template ) base.html基本模板

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM