无法使用Django加载静态文件 [英] Unable to load Static files using Django

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

问题描述

尝试加载静态文件:CSS,使用django静态标签
的Javascript无效,并且大多数CSS也未加载

Tried loading static files: CSS, Javascript using django static tag None of the Javascripts is working, and most of the css are also not loaded

Javascript错误(对于所有JS ):

Error for Javascript(for all JS):


a href = http://127.0.0.1:8000/static/jquery.min.js rel = nofollow noreferrer> http://127.0.0.1:8000/static/jquery.min.js 已加载,即使其MIME类型( text / html)不是有效的JavaScript MIME类型。

The script from "http://127.0.0.1:8000/static/jquery.min.js" was loaded even though its MIME type ("text/html") is not a valid JavaScript MIME type.

Django版本:-3.0.1

Django version: - 3.0.1

我的文件和代码:

Base.html >>

Base.html>>



{% load static %}

<link rel="stylesheet" type="text/css" href={% static 'animate.css' %}>
<script src="{% static 'jquery.min.js' %}"></script>




settings.py >>

settings.py>>



# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
STATIC_ROOT = os.path.join(BASE_DIR, "assets")

我是不使用任何虚拟环境。

I am not using any virtual environment.

我的项目文件夹树。
我的项目文件夹和子目录

My project folder tree. My project Folder and sub directories

推荐答案

将静态文件放入 my_app / static / (而不是创建另一个my_app子目录)中,其中my_app是 project 主文件夹中的一个应用程序文件夹。还可以在静态文件夹中创建一个名称与您的应用程序名称相同的文件夹,并将您的静态文件放置在此处。可以正确地区分您的静态文件
,因此您最终的应用程序目录结构如下:

put your static files in your my_app/static/ (rather than creating another my_app subdirectory), where my_app is an app folder inside the main project folder. also create a folder with name same as that of your app name inside static folder and place your static files there. which would properly distinguish between your static files. so your final app directory structure look like

my_app/
|____static/
     |____my_app/
          |____css/
          |        # your css files
          |____js/
          |        # js files
          |___images/
                   # your image files

并更新您的base.html和

and update your base.html with

<link rel="stylesheet" href="{% static 'my_app/css/main.css' %}" /> 

希望这行得通。

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

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