0

Had a similar question with an older version of Django...

trying to get my site to load with its css and js

This is the structure of my project

gradMVPV1
--> .idea
--> catalog
    views
    models
    apps
    ....
--> gradMVPV1
    settings
    urls
    wsgi
    ....
--> templates
---->static
     css
     js
     ...
---->index.html
db.sqlite3
manage.py

this is what I have on settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'templates/static')

Here is a screenshot of my page

screenshot

Not sure if this helps at all but this is the css on my index.html file

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="static/css/bootstrap.css">
<link rel="stylesheet" href="static/vendors/linericon/style.css">
<link rel="stylesheet" href="static/css/font-awesome.min.css">
<link rel="stylesheet" href="static/vendors/owl-carousel/owl.carousel.min.css">
<link rel="stylesheet" href="static/vendors/lightbox/simpleLightbox.css">
<link rel="stylesheet" href="static/vendors/nice-select/css/nice-select.css">
<link rel="stylesheet" href="static/vendors/animate-css/animate.css">
<!-- main css -->
<link rel="stylesheet" href="static/css/style.css">

I have made these changes based on the answers given

settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
STATIC_URL = 'templates/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'templates/static')

I have also made changes to my index.html file to this effect

{% load staticfiles %}
<!doctype html>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
<link rel="stylesheet" href="{% static 'vendors/linericon/style.css' %}">
<link rel="stylesheet" href="{% static 'css/font-awesome.min.css' %}">
.....

I am however still getting the same issue, the index page loads with not css, js or images

8
  • Can you try removing the first / from /templates/static. That's pointing to the root of your filesystem Commented Dec 1, 2019 at 18:51
  • @anowlinorbit I had already tried that Commented Dec 1, 2019 at 18:59
  • First I recommend using the debug console in your browser and see the exact error, but it is most likely a 404. In your template file, if you're reference a static resource, you need to load the static files with {% load staticfiles %}. Put this at the top of your template file. And to access static content, use <link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}"> where main.css sits in the folder templates/static/css/main.css Commented Dec 1, 2019 at 19:08
  • @anowlinorbit not sure I am reading you correctly, do you mind adding that as an answer Commented Dec 1, 2019 at 19:09
  • 1
    Will do. Also checkout docs.djangoproject.com/en/2.2/howto/static-files Commented Dec 1, 2019 at 19:11

4 Answers 4

2

Try add STATICFILES_DIRS = (os.path.join(BASE_DIR, "templates", "static"),) Your settings file, i read from this document https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-STATICFILES_DIRS

Sign up to request clarification or add additional context in comments.

7 Comments

should I replace STATIC_URL and STATIC_ROOT or add this in addition to these two
@Emm STATIC_URL and STATIC_ROOT in setting.py works well. don't know about the other one
You can keep your STATIC_URL and using href="{% static 'css/style.css' %}>" instead of href="static/css/style.css"> read this template language docs.djangoproject.com/en/2.2/ref/templates/language
And add this to {% load staticfiles %} your html file as guy's answer below i forgot about this
I have tried that, still getting the same issue. Updated my question with what I tried
|
0

If you want to use Django's static files in your templates, then you need to first load the static files with

{% load staticfiles %}

Then, if you wish to use a static item, for example main.css in templates/static/css/main.css you would use this syntax.

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

As an example, here is an edited base.html file of mine:

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
</head>

<body>
<h1>Hello Emm</h1>
</body>
</html>

1 Comment

still getting the same issue...noticed that I get these messages as my terminal updates -> Not Found: /img/f-icons/icon6.png ...this is happening presumably for all css, img and js files. Are there any changes I need to make to settings.py?
0

Check your static url and replace it to STATIC_URL = 'templates/static/' or edit settings.py and put static folder into gradMVPV1 folder.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR,'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
STATIC_URL = '/static/'

You should have this structure
gradMVPV1
--> .idea
--> catalog
    views
    models
    apps
    ....
--> gradMVPV1
    settings
    urls
    wsgi
    ....

---->static
     css
     js
--> templates
     ...
---->index.html
db.sqlite3
manage.py

1 Comment

made these changes, but still getting the same issue. Updated my question
0

Check your static url and replace it to STATIC_URL = '/static/' and edit settings.py.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR,'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
STATIC_URL = '/static/'

You may to have this stucture
project
--> name_project1
    static
       name_project1
           file1.css
           file2.css
    templates
       name_project1
           base.html
           ...
--> project
    settings.py
    __init__.py
    urls.py
    ...
db.sqlite3
manage.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.