Home > Blockchain >  static file css not loading in django
static file css not loading in django

Time:12-17

I have tried everything i know to fix this all static files are working perfectly other than css

views.py

from django.http.response import HttpResponse
from django.shortcuts import render

def index(response):
    return render(response , "main/index.html")



html head

{% extends 'main/base.html' %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href= "{%static "main/css/index.css" %}">

    <title> Home Page   </title>
</head>

Please note that i am using bootstrap in base file if that affects anything

settings.py


STATIC_URL = '/static/'

STATIC_ROOT = "/Users/aryankaushik/Desktop/visual studio code /django/assets"


STATICFILES_DIRS = [
    BASE_DIR / "static",
]



I am sure the folders paths are correct as images are rendering correctly..

Thank you in advance

CodePudding user response:

Bro try adding these:

Setting.py:

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

STATICFILES_DIRS = [
    STA_DIR,
]


Urls.py:

urlpatterns = [
    path('',views.index3,name='index'),
]

View.py:

def index3(request):
    my_dict ={'insert_me':'hello i m views.py and now i m under template/firstone'} 
    return render(request,'firstone/index.html',context=my_dict) 

Index.html:

    <title>Document</title>
    <link rel="stylesheet" href="{% static "css/style.css" %}">
</head>
<body>
    <h1>Hello there Index.HTML here</h1>
    <!-- {{insert_me}} -->
    <img src="{% static "images/kami.jpeg" %}" alt="oh no">

CodePudding user response:

on your settings.py you can place them for your any project this work dynamicly

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

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

your version on html: mistake

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

True version:

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

don't use similar quotes it doesn't work

CodePudding user response:

Try it.

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = [
    BASE_DIR / 'project/static'
]

MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'

views.py

def index(request):
    dict = {'test':'test2'}
    return render(request , "main/index.html", context=dict)

index.html

{% extends 'base.html' %}

{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href= "{% static 'index.css' %}">

    <title>Index.html</title>
</head>


urls.py

urlpatterns = [
    path('',views.index,name='index'),
]
  • Related