静态文件未在Django Python中加载 [英] Static Files not getting loaded in Django Python

查看:83
本文介绍了静态文件未在Django Python中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要运行的代码:

{% 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 for Js and sample data: Reference link

我的静态路径是: C:\ Users \ aims \ Desktop \ mysite \ static

控制台日志中出现以下错误:

I am getting the following error in the console log:

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)

请让我知道我错过了什么.我已经将各自的文件放在静态文件夹中.找不到静止文件.对我来说这是一个谜.帮我解决.

Pease do let me know what I have missed. I have placed the respective files in the static folder. Still file is not found. It is a mystery for me. Help me solve it.

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-找不到静态文件
Django:找不到静态文件
阅读博客: https://www.agiliq.com/blog/2013/03/serving-static-files-in-django/

推荐答案

urls.py

...
if settings.DEBUG:
  urlpatterns +=
    static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

https://docs.djangoproject.com/zh-CN/2.1/howto/static-files/#serving-static-files-during-development

这篇关于静态文件未在Django Python中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆