【发布时间】:2019-05-04 13:11:34
【问题描述】:
这是我要运行的代码:
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="http://misoproject.com/js/d3.chart.js"></script>
</head>
<body>
<script src="{% static 'stock-line.js' %}"></script>
<script src="{% static 'stock-line-app.js' %}"></script>
</body>
</html>
Js和示例数据的参考链接:Reference link
我的静态路径是:C:\Users\aims\Desktop\mysite\static
我在控制台日志中收到以下错误:
GET http://127.0.0.1:8000/static/stock-line.js 404 (Not Found)
GET http://127.0.0.1:8000/static/stock-line-app.js 404 (Not Found)
Cross-Origin Read Blocking (CORB) blocked cross-origin response http://misoproject.com/js/d3.chart.js with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
GET http://127.0.0.1:8000/static/stock-line.js 404 (Not Found)
GET http://127.0.0.1:8000/static/stock-line-app.js 404 (Not Found)
请让我知道我错过了什么。我已将相应的文件放在静态文件夹中。仍然找不到文件。这对我来说是个谜。帮我解决。
Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print("Base URL is \n: ",BASE_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/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 = ['127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'demod3',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/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',
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
print("Statcic URL is : \n: ", STATIC_ROOT)
views.py
def testingD3(request):
return render_to_response('tt/testingD3.html', {})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('tt/', views.show),
path('pie/', views.pie),
path('dd3/', views.testingD3),
path('fera/', views.fera),
]
解决了这些问题:
Django - Static file not found
Django: static file not found
阅读博客:https://www.agiliq.com/blog/2013/03/serving-static-files-in-django/
【问题讨论】:
-
您需要发布您的静态设置和 URL。您是否阅读过有关此主题的许多先前问题中的任何一个?
-
@DanielRoseman 好的,先生,我将编辑我的问题。我虽然这是唯一需要的。忘了其他人可以有不同的设置。
-
您的 STATICFILES_DIRS 设置是什么?
-
@DanielRoseman 即使我有 STATIC_ROOT,我是否需要指定它?
-
是的。 STATIC_ROOT 是您运行 collectstatic 时收集文件到的位置,STATICFILES_DIRS 是收集文件来自的位置。更重要的是,对于您当前的问题,在开发过程中,staticfiles 应用程序将自动从那里提供文件,而不是从 STATIC_ROOT。
标签: javascript python django python-3.x django-staticfiles