简体   繁体   English

Django的静态文件得到404

[英]django static files getting a 404

I can't seem to get django static files working when I visit /static/accept.png it returns a 404. I have the file in project_folder/static 当我访问/static/accept.png时,django静态文件似乎无法正常工作,它返回404。我在project_folder / static中有文件

My installed apps has static files included 我安装的应用包含静态文件

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

and this is what setting up the static files looks like 这就是设置静态文件的样子

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

PROJECT_PATH = os.path.join(BASE_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_PATH = os.path.join(PROJECT_PATH,'static')

STATIC_URL = '/static/' # You may find this is already defined as such.

STATICFILES_DIRS = (
    PROJECT_PATH + STATIC_URL,
)

Also I am trying to get this working locally i'm not worried about getting it to work in production yet. 另外,我正在尝试使它在本地运行,我不担心将其投入生产。

You PROJECT_PATH is wrong. 您的PROJECT_PATH错误。 It points to one level up of the real project path. 它指向实际项目路径的上一级。

Try these settings: 尝试以下设置:

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

STATICFILES_DIRS = (
    STATIC_PATH,
)

Don't get confused with PROJECT_PATH and BASE_DIR, if you want please print and get the values of both the variables, and get the clear picture that how will your code reach the "static" directory. 不要与PROJECT_PATH和BASE_DIR混淆,如果需要,请打印并获取两个变量的值,并清楚地看到代码将如何到达“静态”目录。 Then try the code below 然后尝试下面的代码

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

STATIC_URL = '/static/'

STATICFILES_DIRS = (
                    STATIC_PATH,
)

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

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