admin管理员组文章数量:1023738
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="">
<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="">
<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)
# .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:
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/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/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./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:
Share Improve this question edited Aug 23, 2015 at 9:15 sumit chansouliya asked Aug 23, 2015 at 7:20 sumit chansouliyasumit chansouliya 811 gold badge2 silver badges9 bronze badges 11- use {% load staticfiles %} at the top of your html page – Ajay Gupta Commented Aug 23, 2015 at 7:50
- AjayGupta .I did that . My all Images are loading by same way only css and Js files are not loading. I am using Eclipse IDE for Django. – sumit chansouliya Commented Aug 23, 2015 at 7:54
- where is your STATIC_URL pointing at? and are you running using django server or any other server like apache, nginx? – Ymartin Commented Aug 23, 2015 at 7:54
- I am running my Project at localhost. 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'),) – sumit chansouliya Commented Aug 23, 2015 at 7:56
- are your files in some css and js directory? – Ajay Gupta Commented Aug 23, 2015 at 7:58
4 Answers
Reset to default 2This is for project (mon) statics:
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
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">
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 mand
python manage.py collectstatic
docs : https://docs.djangoproject./en/1.8/ref/contrib/staticfiles/
Hope this helps!
It was my problem too! but i solve it :
- you should go to your project setting
setting.py
and findSTATIC_URL
- note url which is in
STATIC_URL
then change it to the path of absolute static files for example my path isMySite/MyApp/static/My_App/'static files'
- after that copy the path of your
My_app
to theSTATIC_URL
then it should work.
Note: you should insert your path in STATIC_URL
with this format:
/file1/ or `/file1/file2/.../`
hope useful
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="">
<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="">
<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)
# .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:
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/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/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./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:
Share Improve this question edited Aug 23, 2015 at 9:15 sumit chansouliya asked Aug 23, 2015 at 7:20 sumit chansouliyasumit chansouliya 811 gold badge2 silver badges9 bronze badges 11- use {% load staticfiles %} at the top of your html page – Ajay Gupta Commented Aug 23, 2015 at 7:50
- AjayGupta .I did that . My all Images are loading by same way only css and Js files are not loading. I am using Eclipse IDE for Django. – sumit chansouliya Commented Aug 23, 2015 at 7:54
- where is your STATIC_URL pointing at? and are you running using django server or any other server like apache, nginx? – Ymartin Commented Aug 23, 2015 at 7:54
- I am running my Project at localhost. 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'),) – sumit chansouliya Commented Aug 23, 2015 at 7:56
- are your files in some css and js directory? – Ajay Gupta Commented Aug 23, 2015 at 7:58
4 Answers
Reset to default 2This is for project (mon) statics:
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
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">
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 mand
python manage.py collectstatic
docs : https://docs.djangoproject./en/1.8/ref/contrib/staticfiles/
Hope this helps!
It was my problem too! but i solve it :
- you should go to your project setting
setting.py
and findSTATIC_URL
- note url which is in
STATIC_URL
then change it to the path of absolute static files for example my path isMySite/MyApp/static/My_App/'static files'
- after that copy the path of your
My_app
to theSTATIC_URL
then it should work.
Note: you should insert your path in STATIC_URL
with this format:
/file1/ or `/file1/file2/.../`
hope useful
本文标签: javascriptHow to add Css and Js files in Django pythonStack Overflow
版权声明:本文标题:javascript - How to add Css and Js files in Django python - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745590977a2157883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论