Im recently trying out Django again, and I’m running into trouble with loading static files. It seems to be a reoccuring issue accross all projects I create as I’ve followed the steps from https://docs.djangoproject.com/en/4.0/howto/static-files/
here is how my code from “settings.py”, “index.html” look like, please let me know if anymore information is needed.
# settings.py
DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myFirstApp',
]
STATIC_URL = 'static/'
# manual imports
STATICFILES = [
BASE_DIR / "static"
]
Now for the HTML file.
<!-- html file from "../static/home_page/main.css" -->
<!DOCTYPE html>
{% load static %}
<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 'home_page/main.css' %}">
<title>Help Page</title>
</head>
<body>
<header>
<div>
<h3>Welcome to the <span><em>{{help_insert}}</em></span></h3>
</div>
</header>
</body>
</html>
Forgot to add, here’s the error produced
"GET /static/home_page/main.css HTTP/1.1" 404 1813
and here is where the django is trying to search for the folder, I haven’t a clue to change the searching directory
No matching file found for 'home_page/main.css'.
Looking in the following locations:
C:\Users\user\anaconda3\lib\site-packages\django\contrib\admin\static
What directory is that file in?
Is this a development or production environment?
That’s not what I asked.
The file you’re trying to load - main.css
- what directory is it in?
You show the directory name here as home_style
, but your code above references a directory named home_page
.
sorry, that was the wrong project;
here,
Have you made the url settings as described at Serving static files during development?
Ill have a look at that now, I dont think the tutorial im following stated this.
which file should I append this to? Should it be in my Application/Project or both??
I now get the same error, yet with a different code “1914”
"GET /static/home_page/main.css HTTP/1.1" 404 1914
Two things, one I just noticed - the settings is STATICFILES_DIRS
not STATICFILES
.
The other is that the 1914 isn’t a code - it’s the number of bytes in the reply. The code is still a 404.
since changed STATICFILES_DIR to STATICFILES = (os.path.join("static"))
and nothing has changed as of yet, is there anywhere I should alter this?
Thank you for telling me, that was unknown
I don’t understand what you’re saying you did here.
What’s your current STATICFILES_DIRS setting?
(See the sample at Settings | Django documentation | Django)
Should my STATICFILES_DIR link to the root directory of my “static” file like it shows in the link you’ve provided? or should I continue with the "BASE_DIR / ‘static’ " method.
Like you advised, I had changed the variable from STATICFILES_DIR=[...]
to STATICFILES=[...]
You had STATICFILES - that’s what you posted at the top.
It needs to be STATICFILES_DIRS, not STATICFILES and not STATICFILES_DIR.
Okay, i altered the variable name and it works. Thank you very much Ken, I know im stressful to work with thank you for your patience.
Thank you Ken!
No worries! Glad you’ve got it working.
1 Like