在 django FileNotFoundError 上运行 collectstatic 时出错:

问题描述

所以我正在运行命令

python manage.py collectstatic

而且我收到一个错误,我很确定这是因为我的 settings.py 中有一些设置。 首先我会附上一张图片,这样每个人都可以看到我的文档树,然后是错误本身。

知道我做错了什么吗?谢谢。

图片Image with the tree of files and settings

我的settings.py

# on the top
import os

# on the bottom
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR,'staticfiles'))
STATIC_URL = '/static/'


MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')


STATICFILES_Dirs = (
    os.path.join(BASE_DIR,'static'),)

错误如下:

(env) PS C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project> python manage.py collectstatic
Traceback (most recent call last):
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\manage.py",line 22,in <module>
    main()
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\manage.py",line 18,in main
    execute_from_command_line(sys.argv)
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\core\management\__init__.py",line 401,in execute_from_command_line
    utility.execute()
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\core\management\__init__.py",line 395,in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\core\management\base.py",line 330,in run_from_argv
    self.execute(*args,**cmd_options)
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\core\management\base.py",line 371,in execute
    output = self.handle(*args,**options)
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py",line 194,in handle 
    collected = self.collect()
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py",line 109,in collect
    for path,storage in finder.list(self.ignore_patterns):
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\contrib\staticfiles\finders.py",line 130,in list
    for path in utils.get_files(storage,ignore_patterns):
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\contrib\staticfiles\utils.py",line 23,in get_files
    directories,files = storage.listdir(location)
  File "C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project\env\lib\site-packages\django\core\files\storage.py",line 316,in listdir
    for entry in os.scandir(path):
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\lenil\\Documents\\Development\\Personal Projects\\the-journey\\thejourney_project\\thejourney_project\\static'     
(env) PS C:\Users\lenil\Documents\Development\Personal Projects\the-journey\thejourney_project> 

非常感谢您的帮助!

解决方法

您附加的项目结构图像没有显示 staticstaticfiles 的目录。您需要将这些目录向上移动,或者更改您的路径以映射到这些目录。

,

我尝试重新部署,这次使用了这些设置。它对我有用。

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATIC_DIRS = (
    os.path.join(BASE_DIR,'projects/static'),)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

我也不得不将 Procfile 更改为这个

web: python manage.py runserver 0.0.0.0:$PORT

整个步骤:

First make sure you have requirements.txt and Procfile
In Procfile,the contents were like this
web: python manage.py runserver 0.0.0.0:$PORT
And run python manage.py collectstatic just to make sure no errors from collectstatic
Then these commands in cmd
heroku login
Then login from browser
git init
heroku git:remote -a app_name
git add .
git commit -am "first commit"
git push heroku main         -------------– or masterher
heroku run python manage.py migrate
heroku open

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...