Gautam
Gautam

Reputation: 428

Loading CSS returns 404 using django

I am building a chatbot application. The CSS for the frontend is not getting loaded by using django's static data load.

path to css is - C:\Documents\Django_Workspace\bot\templates\static\bot.css

path to html is - C:\Documents\Django_Workspace\bot\templates\index.html

I have added STATIC_URL in my settings file as

STATIC_URL = '/static/'

The HTML link tag looks like

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

My Urls.py looks like

from django.conf.urls import url
from . import views

urlpatterns = [
   url(r'^$', views.index, name='index'),
]

This is how I render the html page

from django.http import HttpResponse
from django.template import loader
from .models import User_Message

def index(request):
   template = loader.get_template('index.html')
   return HttpResponse(template.render())

Can anyone help. I've even tried hardcoding the path of css file in the html tag. But, that's not working

Upvotes: 0

Views: 130

Answers (2)

John Joseph Fernandes
John Joseph Fernandes

Reputation: 369

you need to run the following

python manage.py collectstatic

Upvotes: 1

Paulo Almeida
Paulo Almeida

Reputation: 8061

Your CSS file shouldn't be in the templates folder but in a static folder within the app. From the documentation:

Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg.

See the rest of that section too, it's very succint. You probably want to have a myapp folder inside static, to avoid collisions if you have more apps.

Upvotes: 1

Related Questions