[Answered ]-How to add Css and Js files in Django python

2๐Ÿ‘

  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">
๐Ÿ‘คmadzohan

0๐Ÿ‘

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

๐Ÿ‘คAjay Gupta

0๐Ÿ‘

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!

๐Ÿ‘คYmartin

0๐Ÿ‘

It was my problem too!
but i solve it :

  1. you should go to your project setting setting.pyand 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

๐Ÿ‘คAli Najafi

Leave a comment