我如何收集静态文件?我不能运行这个项目,它会引发错误

问题描述 投票:1回答:1

我正在运行命令python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /static

python manage.py collectstatic

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes 

错误即时通讯;无法打开文件“/static/fonts/FreeSans.ttf”

Copying '/home/mark/Desktop/xls/python-django-exporting-files/static/js/bootstrap.min.js'
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle
    collected = self.collect()
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 107, in collect
    handler(path, prefixed_path, storage)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 315, in copy_file
    self.storage.save(prefixed_path, source_file)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/files/storage.py", line 64, in save
    name = self._save(name, content)
  File "/home/mark/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/files/storage.py", line 223, in _save
    os.makedirs(directory)
  File "/home/mark/.virtualenvs/test/lib/python2.7/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/home/mark/.virtualenvs/test/lib/python2.7/os.py", line 157, in makedir
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/static'

进入浏览器时出错;

Can't open file "/static/fonts/FreeSans.ttf"
TTFError at /
Can't open file "/static/fonts/FreeSans.ttf"
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.8.2
Exception Type: TTFError
Exception Value:    
Can't open file "/static/fonts/FreeSans.ttf"
python django
1个回答
2
投票

STATIC_ROOT = os.path.join(BASE_DIR, '/static/')应该是:

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

/重置树,它在根中寻找静态目录,即字面意思:/static你想要像/home/user/project/static这样的东西

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, 'static/') 
STATICFILES_DIRS = ( "/home/mark/Desktop/xls/python-django-exporting-files/static‌​", ) 
try: 
  from local_settings import * 
except: 
  pass

STATICFILES_DIRS是一个保存自己的静态文件的地方,应该使用collectstatic收集这些文件。它不能与STATIC_ROOT相同。

你的静态结构就像

DJANGO_PROJECT_DIR
|--> PROJECT_DIR
|----> settings.py
|----> templates
|----> static
|--> static
|--> manage.py

然后代码将(用项目目录名替换PROJECT_DIR):

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, 'static') 
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'PROJECT_DIR', 'static‌'), ) 
try: 
  from local_settings import * 
except: 
  pass
© www.soinside.com 2019 - 2024. All rights reserved.