0

the html page appear without any style or images because of not finding the static files in Django

the load :

{%  load staticfiles %}

this is the static call :

<link rel="stylesheet" href="{% static  'fonts/icomoon/style.css' %}">

the url setup :

STATIC_URL = os.path.join(BASE_DIR, "static/")
4
  • 2
    Django? You tagged this question with flask. What are you using, Django or flask?
    – darksky
    Commented Feb 15, 2019 at 7:31
  • 2
    Why static url is pointing to the directory, it should be STATIC_URL = '/static/' Commented Feb 15, 2019 at 7:36
  • Try: {% load static %}
    – TechSavy
    Commented Feb 15, 2019 at 7:36
  • 2
    Please show your settings.py and your html page Commented Feb 15, 2019 at 7:48

2 Answers 2

1

So let me give you a working sample. Your settings.py could be like this

STATIC_URL = '/static/'

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

Up here, I have a folder in my project root called 'static' which contains folder like 'css', 'js' etc. You also need to set this up in your urls.py so it can be accessible to the template. So I would do it like this:

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls.static import static
    from django.conf import settings

    urlpatterns = [

        path('', user_login),
        path('admin/', admin.site.urls), 
    ] 

    urlpatterns += staticfiles_urlpatterns()

Then at the top of your HTML page, import the static files

{% load static from staticfiles %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css'%}">
<link rel="stylesheet" href="{% static 'css/style.css'%}">

I hope this helps

1

You gave base path to Static_url.

Try these settings:

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

For Django 2.0, loading static files in html requires new load syntax:

{% load static %}

Not the answer you're looking for? Browse other questions tagged or ask your own question.