Python Can';t在克隆Django项目后加载静态文件
我知道这个问题已经被问了很多,但我真的不明白为什么我的静态文件不会被加载。我从一个朋友那里克隆了一个项目(不知道这是否与此有关),当尝试链接到静态文件时,我在Chrome上的调试控制台中不断收到404错误 我的HTML中的标题Python Can';t在克隆Django项目后加载静态文件,python,django,django-staticfiles,Python,Django,Django Staticfiles,我知道这个问题已经被问了很多,但我真的不明白为什么我的静态文件不会被加载。我从一个朋友那里克隆了一个项目(不知道这是否与此有关),当尝试链接到静态文件时,我在Chrome上的调试控制台中不断收到404错误 我的HTML中的标题 {% load staticfiles %} {% load static %} <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></s
{% load staticfiles %}
{% load static %}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<!--http://ianlunn.github.io/Hover/-->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="{% static 'css/hover.css' %}" rel="stylesheet" media="all">
<link href="css/hover.css" rel="stylesheet" media="all">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/bootstrap-theme.css' %}">
<link rel="stylesheet" href="{% static 'css/bootstrap-override.css' %}">
</head>
import os
import datetime
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DJANGO_DIR = os.path.dirname(BASE_DIR)
PROJECT_DIR = os.path.dirname(os.path.dirname(DJANGO_DIR))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ****
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.sites",
#3rd Party APPS
'rest_framework',
'rest_framework_jwt',
'cuser', #https://github.com/tmm/django-username-email
"pinax.stripe",
#Custom modules
'api',
#Own apps
'apps.user',
'apps.payment',
]
# Custom user model
AUTH_USER_MODEL = 'user.User'
# SITE_ID for the Sites framework
SITE_ID = 1
PINAX_STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY", "**")
PINAX_STRIPE_PUBLIC_KEY = os.environ.get("STRIPE_PUBLIC_KEY", "**")
JWT_AUTH = {
'JWT_ALLOW_REFRESH': True,
'JWT_EXPIRATION_DELTA': datetime.timedelta(days=14)
}
DEBUG_TOOLBAR_PANELS = [
'debug_toolbar.panels.signals.SignalsPanel',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'audit_log.middleware.UserLoggingMiddleware',
]
ROOT_URLCONF = 'core.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(DJANGO_DIR, 'templates'), os.path.join(DJANGO_DIR, 'email_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',
'django.template.context_processors.media',
],
},
},
]
WSGI_APPLICATION = 'core.wsgi.application'
REST_FRAMEWORK = {
# 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'DEFAULT_AUTHENTICATION_CLASSES': (
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = '/project/static/'
MEDIA_URL = 'django/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'django/')
我想我什么都试过了,加载MEDIA\u URL
和MEDIA\u URL
这是我的项目结构的截图
试试这个:
STATIC\u ROOT=os.path.join(BASE\u DIR,“/project/STATIC/”)
静态文件可能很棘手,并且有很多原因导致它们无法加载。你说你的媒体文件加载正确,因为你引用了它们,比如media\u ROOT=os.path.join(BASE\u DIR,'django/')
。如果您要打印该路径(Print MEDIA\u ROOT
),控制台中将出现类似的内容:“ThisPC/Documents/Github/django/MEDIA”。此路径直接指向您的媒体文件夹,但当django查找静态根目录时,它找不到该文件夹,因为您只说了“project/Static”,但它应该是“ThisPC/Documents/Github/project/Static”(或类似于此)
如果你问你的朋友为什么这样做,那是因为你只在生产时上传GitHub文件夹,那里的静态路径是正确的。媒体文件在生产中不会像这样工作
法齐尔·扎伊德在评论中指出的也很重要。在URL.py文件中,您有if common.DEBUG:
,通常您会使用if settings.DEBUG:
(对于settings.py文件中的DEBUG=True
)更改该行或找出common.py是什么以及他为什么使用它。这似乎是一个额外的设置文件。如果您不熟悉该项目,我也不建议您使用它
在将该项目推向生产之前,您需要更改密钥
,并为静态文件安装。我希望这会有帮助,也会起作用
p、 这到底是什么?
PINAX\u STRIPE\u SECRET\u KEY=os.environ.get(“STRIPE\u SECRET\u KEY”、“sk\u test\u 7dTOnMGX55bNC2yQ4ihqsHuV”)
PINAX\u STRIPE\u PUBLIC\u KEY=os.environ.get(“STRIPE\u PUBLIC\u KEY”、“pk\u test\u LutZeiAuAewyQhJuLLUJXdfi”)
并且您还发布了项目的密钥?你知道当它只是一个爱好网站,你可以练习它的所有乐趣和游戏,但当涉及付款时,你可能会因为在网上发布类似的东西而被起诉。如果一个用户的银行账户信息遭到黑客攻击(你提供的所有密码都很容易被黑客攻击),你就会遇到巨大的问题。请记住在将任何内容推送到网上之前更改所有这些内容。快乐编码:)
编辑:
试试这个:
STATIC\u ROOT=os.path.join(os.path.dirname(BASE\u DIR),'/project/STATIC')
或硬编码STATIC\u ROOT=users/elitebook/documents/github/project/STATIC/
路径应该是正确的。这肯定是你的问题,或者至少是主要问题之一。Django不知道在哪里查找静态文件。如果无法复制打印介质路径,请对其进行编辑,然后将硬编码路径放入设置文件中。这应该是路径:
STATIC\u ROOT=users/elitebook/documents/github/project/STATIC/
不确定aC://
是否在该部分前面,或者不查看媒体根,尝试复制第一部分并编辑第二部分,使其指向STATIC而不是Media。当Debug为True且您的文件仍然没有加载时,您在其他地方会遇到另一个问题。请尝试以下操作:STATIC\u ROOT=os.path.join(BASE\u DIR,“/project/STATIC/”)
静态文件可能很棘手,并且有很多原因导致它们无法加载。你说你的媒体文件加载正确,因为你引用了它们,比如media\u ROOT=os.path.join(BASE\u DIR,'django/')
。如果您要打印该路径(Print MEDIA\u ROOT
),控制台中将出现类似的内容:“ThisPC/Documents/Github/django/MEDIA”。此路径直接指向您的媒体文件夹,但当django查找静态根目录时,它找不到该文件夹,因为您只说了“project/Static”,但它应该是“ThisPC/Documents/Github/project/Static”(或类似于此)
如果你问你的朋友为什么这样做,那是因为你只在生产时上传GitHub文件夹,那里的静态路径是正确的。媒体文件在生产中不会像这样工作
法齐尔·扎伊德在评论中指出的也很重要。在URL.py文件中,您有if common.DEBUG:
,通常您会使用if settings.DEBUG:
(对于settings.py文件中的DEBUG=True
)更改该行或找出common.py是什么以及他为什么使用它。这似乎是一个额外的设置文件。如果您不熟悉该项目,我也不建议您使用它
在将该项目推向生产之前,您需要更改密钥
,并为静态文件安装。我希望这会有帮助,也会起作用
p、 这到底是什么?
PINAX\u STRIPE\u SECRET\u KEY=os.environ.get(“STRIPE\u SECRET\u KEY”、“sk\u test\u 7dTOnMGX55bNC2yQ4ihqsHuV”)
PINAX\u STRIPE\u PUBLIC\u KEY=os.environ.get(“STRIPE\u PUBLIC\u KEY”、“pk\u test\u LutZeiAuAewyQhJuLLUJXdfi”)
并且您还发布了项目的密钥?你知道当它只是一个爱好网站,你可以练习它的所有乐趣和游戏,但当涉及付款时,你可能会因为在网上发布类似的东西而被起诉。如果一个用户的银行账户信息遭到黑客攻击(你提供的所有密码都很容易被黑客攻击),你就会遇到巨大的问题。请记住在将任何内容推送到网上之前更改所有这些内容。快乐编码:)
编辑:
试试这个:
STATIC\u ROOT=os.path.join(os.path.dirname(BASE\u DIR),“/project/STATIC”)
或硬编码STATIC\u ROOT=users/elitebook/documents/github/project/stat
urlpatterns = [
url(r'^', include(auth_urls)),
url(r'^', include(user_urls)),
url(r'^', include(referral_urls)),
url(r'^', include(payment_urls)),
url(r'^', include(cabinet_urls)),
# Admin
url(r'^admin/', admin.site.urls),
url(r'^api/feedback/', FeedbackView.as_view()),
]
if common.DEBUG:
urlpatterns += static(common.MEDIA_URL, document_root=common.MEDIA_ROOT)
urlpatterns += static(common.STATIC_URL, document_root=common.STATIC_ROOT)