Abanoub Magdy
Abanoub Magdy

Reputation: 3

static files in django is not working (404) although the path is right

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/")

Upvotes: 0

Views: 416

Answers (2)

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

Upvotes: 1

ans2human
ans2human

Reputation: 2357

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 %}

Upvotes: 1

Related Questions