Django — Can't get static CSS files to load

后端 未结 20 1689
借酒劲吻你
借酒劲吻你 2020-11-29 23:45

I\'m running Django\'s development server (runserver) on my local machine (Mac OS X) and cannot get the CSS files to load.

Here are the relevant entries

相关标签:
20条回答
  • 2020-11-30 00:04

    Add the following code to your settings.py:

    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    

    After that, create the static folder at the root directory of your project.

    To load the static files on templates use:

    {% load static %}
      <img src="{% static "images/index.jpeg" %}" alt="My image"/>
    
    0 讨论(0)
  • 2020-11-30 00:06

    These steps work for me, just see Load Static Files (CSS, JS, & Images) in Django

    I use Django 1.10.

    1. create a folder static on the same level of settings.py, my settings.py's path is ~/djcode/mysite/mysite/settings.py, so this dir is ~/djcode/mysite/mysite/static/;
    2. create two folders static_dirs and static_root in static, that's ~/djcode/mysite/mysite/static/static_dirs/ and ~/djcode/mysite/mysite/static/static_root/;
    3. write settings.py like this:

      # Static files (CSS, JavaScript, Images)
      
      # https://docs.djangoproject.com/en/1.10/howto/static-files/
      
      STATIC_URL = '/static/'
      
      STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'static', 'static_root')
      
      STATICFILES_DIRS = (
          os.path.join(BASE_DIR, 'mysite', 'static', 'static_dirs'),
      )
      
    4. do this command $ python manage.py collectstatic in shell;

    5. create a folder css in static_dirs and put into your own .css file, your css file' path is ~/djcode/mysite/mysite/static/static_dirs/css/my_style.css;

    6. change <link> tag in .html file: <link rel="stylesheet" type="text/css" href="{% static 'css/my_style.css' %}">,

    Finally this link's path is http://192.168.1.100:8023/static/css/my_style.css

    Bingo!

    0 讨论(0)
  • 2020-11-30 00:06

    See if your main application (where the static directory is located) is included in your INSTALLED_APPS.

    Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

    0 讨论(0)
  • 2020-11-30 00:07

    For recent releases of Django, You have to configure static files in settings.py as,

    STATIC_URL = '/static/' # the path in url
    
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    

    and use it with static template tag,

    {% load static %}
    <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
    
    0 讨论(0)
  • 2020-11-30 00:07

    I had to use

    STATICFILES_DIRS = ( '/home/USERNAME/webapps/django/PROJECT/static/', )
    

    That helped me.

    0 讨论(0)
  • 2020-11-30 00:09

    Two most Basis points to be noted for running Static files in Django Application are - Declare static file path in your settings.py file

    STATIC_URL = '/static/'
    

    Another important parameter is the web page in which you are using static keyword, you need to load the static files.

    {% load static %}
    
    0 讨论(0)
提交回复
热议问题