Django collectstatic:是否可以访问Django项目之外的静态文件?

2024-10-02 12:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从django应用程序访问静态文件,以访问django应用程序之外的静态文件。 我想将UIUX/static中的文件复制或访问到DjangoRepo/myapp/static。 它看起来像下面

UIUX/
 - static
  - staticfile...
 - templates
  - index.html
DjangoRepo/
 - myapp
  - static
  - templates

我的设置文件

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

STATIC_URL = '/static/'

# trying to access the static file outside of django file 
STATICFILES_DIRS = (
    #os.path.join(PROJECT_ROOT, 'static/'),
    "C:/Users/kevin/workspace/UIUX/static"
)

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

当我运行python manage.py collectstatic时,我得到了这个

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 188, in handle
    collected = self.collect()
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 105, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\contrib\staticfiles\finders.py", line 131, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\contrib\staticfiles\utils.py", line 23, in get_files
    directories, files = storage.listdir(location)
  File "C:\Users\iact80h\.virtualenvs\lynx-ocr-python-services-6157-7DFxjqs2\lib\site-packages\django\core\files\storage.py", line 315, in listdir
    for entry in os.scandir(path):
FileNotFoundError: [WinError 3] 指定されたパスが見つかりません。: 'C:\\Users\\kevin\\workspace\\lynx-ocr-python-services-6157\\C'

甚至可以访问项目外部的静态文件吗


Tags: djangoinpylibpackagesservicelinesite
1条回答
网友
1楼 · 发布于 2024-10-02 12:29:49

is it possible to access static files that are outside of Django Project?

对!!您可能想从Django的官方文档中了解STATICFILES_DIRS


研究以下代码:

my_str = (
    'My Str'
)
print(type(my_str))    # Prints: <class 'str'>

my_typle = (
    'My Tuple',
)
print(type(my_typle))    # Prints: <class 'tuple'>


STATICFILES_DIRS必须是list(或tuple),而不是str

因此,尝试改变:

STATICFILES_DIRS = (
    #os.path.join(PROJECT_ROOT, 'static/'),
    "C:/Users/kevin/workspace/UIUX/static"
)

致:

STATICFILES_DIRS = (
    #os.path.join(PROJECT_ROOT, 'static/'),
    "C:/Users/kevin/workspace/UIUX/static",    # Note the , at the end.
)

相关问题 更多 >

    热门问题