我试图遵循this article来将Django / Vue应用程序部署到heroku。概述的步骤是:
TEMPLATES = [...]
部分中,添加'DIRS':[os.path.join(BASE_DIR, 'dist')],
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'dist/static')],
(也在settings.py中)url(r'^$', TemplateView.as_view(template_name='index.html')),
我现在已经完成了所有这些操作,这是我清理过的settings.py文件:
"""
Django settings for bad_fortune_cookie project.
Generated by 'django-admin startproject' using Django 2.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
import dj_database_url
#from whitenoise.django import DjangoWhiteNoise
from rest_framework import *
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com','localhost']
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'bad_fortune_cookie',
#'fortunes'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'bad_fortune_cookie.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'dist')],
'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',
],
},
},
]
WSGI_APPLICATION = 'bad_fortune_cookie.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
# Pagination
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
# Password validation
# https://docs.djangoproject.com/en/2.1/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/2.1/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/2.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'dist/static')
]
但是,即使在运行collectstatic之后,我的新文件仍显示404:
我查看了一些看似相关的问题(某些问题比其他问题更相关),但是尝试使用这两个答案中的特定解决方案并不能解决问题: Error 404 static files in django 2.0
感谢您的帮助,如果有帮助,则回购代码为here.
答案 0 :(得分:0)
在模板顶部添加{% load staticfiles %}
,并将ypur静态文件保留在dist/static
下。
{% load staticfiles %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
</head>
<body>
</body>
</html>
假设我在目录bootstrap.css
中有dist/static/css/bootstrap.css
。
答案 1 :(得分:0)
从github的实现中可以看到,您需要在项目中定义资产目录,如下所示(从代码粘贴):
module.exports = {
outputDir: 'dist',
assetsDir: 'static',
// baseUrl: IS_PRODUCTION
// ? 'http://cdn123.com'
// : '/',
// For Production, replace set baseUrl to CDN
// And set the CDN origin to `yourdomain.com/static`
// Whitenoise will serve once to CDN which will then cache
// and distribute
devServer: {
proxy: {
'/api*': {
// Forward frontend dev server request for /api to django dev server
target: 'http://localhost:8000/',
}
}
}
}
基本上,这里发生的是,您的静态文件存储在根路径/
中,但是静态文件应在路径/static
(即<your server>/static/js/abc.js
)中提供服务>