简体   繁体   中英

How to add Css and Js files in Django python

I am facing to load static only css and Javascript in my django project. By this code image files are loading but only js and css files are not loading

 {% load staticfiles %}
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>Home</title>
 <link href="{% static "pro.css" %}" rel="stylesheet" type="text/css"/>
 </head>
 <body>
 <div id="sd">
 <h1>Hi my first django application</h1>
 <img src="{% static "images/img08.jpg" %}"/>
 <div id="demo"></div>
 </div>
 </body>
 </html>

and my page source in browser

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Home</title>
<link href="/static/pro.css rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="sd">
<h1></h1> 
<img src="/static/images/img08.jpg"/>
<div id="demo"></div>
</div>
</body> 
</html>

my setting files code`

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS=     (os.path.join(BASE_DIR,'questions','static','css'),os.path.join(BASE_DIR,'questions','static','js'),)

I got this error message when i checked in my Javascript console - 127.0.0.1/:19 Resource interpreted as Stylesheet but transferred with MIME type application/x-css:

Try this :

<link href="{% static "css/pro.css" %} rel="stylesheet" type="text/css"/>

same applies for js.

You have given path for images as static "images/image_name.jpg" but missing the same for css and js files

I guess what you are missing is collectstatic

run this command

python manage.py collectstatic

docs : https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/

Hope this helps!

  1. This is for project (common) statics:

     STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) 
  2. And this is for your apps statics:

     STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) 

Then in template you can access it using:

{% load static %}

{# this is in project static dir == tip №1 == static/... #}

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



{# suppose your app name is `questions` == tip №2 == questions/static/questions/... #}    

<script type="text/javascript" src="{% static 'questions/js/some.js' %}"></script>
<link href="{% static 'questions/css/pro.css' %}" rel="stylesheet" type="text/css">

It was my problem too! but i solve it :

  1. you should go to your project setting setting.py and find STATIC_URL
  2. note url which is in STATIC_URL then change it to the path of absolute static files for example my path is MySite/MyApp/static/My_App/'static files'
  3. after that copy the path of your My_app to the STATIC_URL then it should work.

Note: you should insert your path in STATIC_URL with this format:

/file1/ or `/file1/file2/.../`

hope useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM