3

I've recently started learning Django and I've been having a lot of issues with implementing css into my code. I've found a lot of people with the same issue as me but I am still unable to find an answer.

Currently when I run open the website it give me this https://i.sstatic.net/cL1I3.jpg

And I'm not sure if this is because of the settings, the actual css or something in the html.

My html boilerplate

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>homepage</title>
<link rel="stylesheet" type="test/css" href="{% static'css/home.css' %}"/>
</head>

settings.py

DEBUG = True
STATIC_ROOT = ''

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

STATIC_URL = '/static/'

views.py

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

from leatherbiscuit.views import index

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
]
if settings.DEBUG:
    urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)   

urls.py

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import TemplateView

    
def index(request):
    return render(request, 'index.html')

def home_view(request):
return HttpResponse(request, 'index.html') 

1 Answer 1

2

You need to add a space between the name of the template tag and its parameters, so it should be:

         ↓ a space between static and 'css/home.css'
{% static 'css/home.css' %}

The parser of the Django template engine has some peculiarities. For example, a tag should not be spanned over multiple lines, and like in Python's method calls one should first list the positional parameters and then the named parameters.

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

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.