简体   繁体   English

Django上的静态文件设置和目录结构

[英]static files settings and directory structures on Django

I read Django Manual's this page and I decided to move my static directory. 我阅读了Django手册的本页,然后决定移动静态目录。 After I moved static directory and open a page, an error occurs on a browser. 移动静态目录并打开页面后,浏览器将发生错误。

FileNotFoundError at /myapp/ [Errno 2] No such file or directory: '/home/jap/git/mysite/static' / myapp / [Errno 2]处的FileNotFoundError没有此类文件或目录:'/ home / jap / git / mysite / static'

Here is my old directories. 这是我的旧目录。

mysite/    
|---manage.py    
|---mysite/
|---myapp/
|---static/
    |----test.css

Then, here is my new directories. 然后,这是我的新目录。

mysite/
    manage.py
    mysite/
    myapp/
        static/
            myapp/
                test.css

settings.py settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'debug_toolbar',
]

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

# cf
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

If you give me some pieces of advice, I would be thankful. 如果您给我一些建议,我将不胜感激。 Thank you. 谢谢。

Django: 1.9.1 Python: 3.5.1 Django的:1.9.1 Python的:3.5.1

In your settings.py , you define where Django should lookup to search for your static files. settings.py ,定义Django应该在哪里查找以搜索您的静态文件。

Right now, you define the folders it should lookup as 现在,您定义应该查找为的文件夹

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

which is basically STATICFILES_DIRS = ("/home/jap/git/mysite/static/",) 基本上是STATICFILES_DIRS = ("/home/jap/git/mysite/static/",)

But it its located at /home/jap/git/mysite/myapp/static/ and not /home/jap/git/mysite/static/ as configured, because you moved you static folder into your app folder. 但它位于配置的/home/jap/git/mysite/myapp/static/而不是/home/jap/git/mysite/static/ ,因为您已将static文件夹移到了app文件夹。

So you should define your STATICFILES_DIRS as 因此,您应该将STATICFILES_DIRS定义为

APP_DIR = os.path.join(BASEDIR, 'myapp')
STATICFILES_DIRS = (
    os.path.join(APP_DIR, 'static'),
)

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

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