在 django FileNotFoundError 上运行 collectstatic 时出错:

如何解决在 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?