首页 > 解决方案 > 使用 Apache 服务时找不到 Django 文件

问题描述

我使用 Linode 在 Linux 服务器上部署了我的 Django 项目。当我通过python manage.py runserver端口 8000 上的命令运行它时,一切看起来都很好。但是,当我使用 apache2 时,加载网站时出现以下错误:

FileNotFoundError at /
[Errno 2] No such file or directory: 'research_files/data.xlsx'
Request Method: GET
Request URL:    http://453.33.13.202/
Django Version: 3.1.5
Exception Type: FileNotFoundError
Exception Value:    
[Errno 2] No such file or directory: 'research_files/data.xlsx'

在 /etc/apache2/sites-available 我创建了一个 django_project.conf 文件,其中包含以下说明:

Alias /research_files/ /home/alexa/django_project/research_files/
<Directory /home/alexa/django_project/django_project/research_files>
    Require all granted
</Directory>

<Directory /home/alexa/django_project/django_project>
    <Files wsgi.py>
          Require all granted
    </Files>
</Directory>

WSGIScriptAlias / /home/alexa/django_project/django_project/wsgi.py>
WSGIDaemonProcess django_app python-path=/home/alexa/django_project python-home=/home/alexa/django_project/venv
WSGIProcessGroup django_app

然后我通过运行启用了该文件,sudo a2ensite django_project并通过运行授予了对该文件夹的访问权限:

sudo chown :www-data django_project/research_files
sudo chmod 664 django_project/research_files

什么可能导致此错误?

标签: djangoapachedjango-staticfiles

解决方案


也许是在 research_files 之后的第一行中的额外 /,因此 /research_files/ 将是 /research_files。这也可能是一个指南https://stackoverflow.com/a/5684257/15020017


推荐阅读