I have tried everything suggested on the entire internet and stuck here for over 5 days Here are my settings.py
STATIC_URL = '/static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I have a staticfile directory, i created with
python manage.py collectstatic
and I have all my css in that file
here are my urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls')),
]
urlpatterns = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here are my app urls
from django.urls import path
from accounts.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', SignUpView.as_view(), name='signup'),
path('otp/', OTPView.as_view(), name='otp'),
path('login/', LoginView.as_view(), name='login'),
path('home/<int:id>', HomeView.as_view(), name="home"),
path('logout/', UserLogout.as_view(), name="logout"),
]
urlpatterns = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and here is my template base.html that I am inheriting in every other template
{% csrf_token %}
{% load crispy_forms_tags %}
<!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="/User_Authentication/staticfiles/admin/css/responsive.css">
</head>
I am getting this error
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:06:31] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Why am i still not able to use staticfiles
CodePudding user response:
From the docs, static-files
{% load static %}
{% csrf_token %}
{% load crispy_forms_tags %}
<!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 'admin/css/responsive.css' %}">
</head>
You need to load it first, the order of loading these three might have to be altered.
You can run the app, go to the page, inspect and find out exactly what url django translates this into. You are basically asking django/jinja to dynamically convert this static path to the corresponding static url instead of hardcoding it.
Note: {% load static %}
needs to be added in every template file that uses "{% static '...' %}"
. Just adding it in base.html
or an equivalent file that other templates extend from is not going to work.