简体   繁体   English

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

[英]Django collectstatic : is it possible to access static files that are outside of Django Project?

I am trying to access the static files from my django app to the static files that are outside of my django app.我正在尝试从我的 django 应用访问 static 文件到我的 django 应用之外的 static 文件。 I want to copy or access the files in UIUX/static to DjangoRepo/myapp/static.我想将 UIUX/static 中的文件复制或访问到 DjangoRepo/myapp/static。 it looks like below看起来像下面

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

my setting file我的设置文件

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'

when I run python manage.py collectstatic I get this当我运行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'

is it even possible to access the static files that are outside of the project?甚至可以访问项目外部的 static 文件吗?

is it possible to access static files that are outside of Django Project?是否可以访问 Django 项目之外的 static 文件?

Yes!是的! You might want to read about STATICFILES_DIRS from the Django's official documentation.您可能想从 Django 的官方文档中阅读有关STATICFILES_DIRS的信息。


Study the below code:研究下面的代码:

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

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


STATICFILES_DIRS needs to be a list (or tuple ), not a str . STATICFILES_DIRS需要是一个list (或tuple ),而不是一个str

So, try changing:所以,尝试改变:

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

To:到:

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

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

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