I have a html file named index.html in template folder and two css file computer.css and mobile.css both are in static folder. How can i use them in a single index.html file.
1 Answer
Just add them as you normally do in HTML. But for static files you have to include {% load static %} in your HTML file and when you access the URL for css files do it in the django way like this:
index.html
{% load static %}
<html>
<head>
<title>...</title>
<link rel="stylesheet" href="{% static './path/to/css' %}">
<link rel="stylesheet" href="{% static './path/to/another/css' %}">
</rest of the code>...
TL/DR: In production static files like CSS is supposed to be handled by proxy server like nginx. So if you are in development stage, your server may not resolve directory for static folder. For that you can manually add the following code in your project's urls.py
from django.urls import re_path
from django.views.static import serve
from django.conf import settings
urlpattern += [
re_path(r'^static/(?:.*)$', serve, {'document_root': settings.STATIC_ROOT, })
]
And define STATIC_ROOT in your settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')