简体   繁体   English

Django-找不到404静态文件

[英]Django - 404 static files not found

I know theres about a millon of these questions but non of them have helped me out. 我知道这些问题有千篇一律,但没有一个问题帮助了我。

I can't access my static files for deployment, I've slpit my settings file into base and production and put them in a folder in the settings.py orginal file. 我无法访问要部署的静态文件,我已经将设置文件打入基础和生产环境,并将它们放在settings.py原始文件的文件夹中。

I've done everything neccessary but it still doesn't seem to be working and I can't work it out for the life of me. 我已经完成了所有必要的工作,但它似乎仍然无法正常工作,我无法终生解决。

I've tried editing the path several time and no change. 我试过几次编辑路径,没有任何更改。

Maybe I've missed something obvious and someone else can see it. 也许我错过了一些显而易见的事情,其他人也可以看到。

  1. venv 静脉
  2. --project - 项目
  3. ----app1 ---- app1
  4. ------static folder ------静态文件夹
  5. ----wsgi folder ---- wsgi文件夹
  6. ------settings_old.py ------settings_old.py
  7. ------new settings folder ------新设置文件夹
  8. ---------base.py ---------base.py
  9. ---------production.py ---------production.py

base.py base.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = ['django.contrib.staticfiles',]

STATIC_URL = '/static/'

STATIC_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_r')

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

I've tried a couple of different things like changing from BASE_DIR to a new PROD_DIR that directly goes to the static file in index. 我尝试了几种不同的操作,例如从BASE_DIR更改为直接进入索引中的静态文件的新PROD_DIR。

PROD_ROOT = os.path.dirname(os.path.abspath('__file__' ))
PROD_ROOT= os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath('__file__' ))))
PROD_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

Nothing seems to be working. 似乎没有任何作用。

UPDATE: 更新:

as suggested by other I've tried collectstatic, the folder env/project/static_r was found however, i get 正如其他人建议的那样,我尝试了collectstatic,但是发现文件夹env / project / static_r

'0 files where copied to static_r'. '0个文件复制到static_r'。

furthermore, also as suggested I printed out base_dir and prod_dir and got the following 此外,还按照建议,我打印出base_dir和prod_dir并得到以下内容

"prod is /user/venv/project" & "base is /user/venv/project/wsgi-folder" “产品是/ user / venv / project”和“基础是/ user / venv / project / wsgi-folder”

ps this is after I edited prod_dir to - ps这是我将prod_dir编辑为-

PROD_ROOT = os.path.dirname(os.path.abspath('__file__' ))

I am not really sure, but are you sure it is STATIC_DIRS and not STATICFILES_DIRS? 我不太确定,但是您确定它是STATIC_DIRS而不是STATICFILES_DIRS吗? '-' '-'

Based on your settings, your static files should be located in /project/static/ instead of in the individual app. 根据您的设置,您的静态文件应位于/ project / static /中,而不是单个应用程序中。

You will also need to load the static files as stated in the documentation 您还需要按照文档中的说明加载静态文件

{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>

What you can always do to test your paths is to print it. 您始终可以测试路径的方法是打印它。

so in your settings do 所以在您的设置中

print BASE_DIR, STATIC_DIRS, STATIC_ROOT

and you'll see where your error is. 您会看到错误在哪里。

In my opinion it's the fact you're putting the static folder within the app, rather than within the project, because collectstatic will put all static files in one directory regardless 我认为这是因为您将静态文件夹放在应用程序中而不是项目中,因为collectstatic会将所有静态文件放在一个目录中,无论

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

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