Unable to load static files in Django Production - python

I am trying to run the app with DEBUG=False
Below is my setting file configuration
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/")]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
After running python manage.py collectstatic, all the static files in the app path are copied to staticfiles directory (mentioned in STATIC_ROOT path).
When loading the webpage, the static files are failed to get load.
Error message:
GET /static/dist/bootstrap-4.0.0-dist/js/bootstrap.min.14d449eb8876.js HTTP/1.1" 404 77
GET /static/dist/bootstrap-select/js/bootstrap-select.min.31f649694651.js HTTP/1.1" 404 77
GET /static/js/base.1332bbb46ac5.js HTTP/1.1" 404 77
GET /static/crsummary/dist/amcharts4/core.ea1ec0eb6727.js HTTP/1.1" 404 77
Looking at the error message, apps is trying to load the bootstrap.min.14d449eb8876.js from path /static/\*/\* but the actual file location is staticfiles/\*/\*
I am not sure what configuration that I have missed out here.

Django doesn't allow to load staticfiles when codes in production, instead of using aws or any other online content delivery network
If you really wan't to load staticfiles from the codes directory , you need to install whitenoise , and define it in your middleware, staticfiles, and installed_apps
for more you can search for use and settings whitenoise in django

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href=" {%static 'app1/css/freelancer.css' %}" />
Load the static files like this.
{% load static %}
<img class="img-fluid" src="{% static 'app1/img/portfolio/ai.png' %}" alt="">
Load the image like this.
Before that , Required to specify the Base directory and static directory
STATIC_DIR=os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS=[STATIC_DIR,]
Base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Build paths inside the project like this: os.path.join(BASE_DIR, ...)

Related

Getting 'GET /static/css/base.css HTTP/1.1" 404 1795' error for static files

I have no idea what I'm doing wrong. I have STATIC_URL = '/static/' STATICFILES_DIR = [str(BASE_DIR.joinpath('static'))] under my settings.py. Here is an image to my current file structure https://pasteboard.co/K3uhtSN.png I linked with <link rel="stylesheet" href="{% static 'css/base.css' %}"> in base.html and loaded at the very top using {% load static %} Thank you
I had the same problem, but it solved by writing "staticfiles_dirs" like this:
STATICFILES_DIRS = ((os.path.join(BASE_DIR, 'static')), )
I had the same problem. If you had created a 'staticfiles' folder or directory during production then be sure to run python manage.py collectstatic in your command prompt or terminal.

DJango not finding static files, despite STATIC_URL and STATIC_ROOT set

I have just started learning DJango and am trying to use css file for my html template. As per the documentation, I have added the STATIC_URL and STATIC_ROOT to my settings.py file as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
BASE_DIR is already set as follows:
BASE_DIR = Path(__file__).resolve().parent.parent
In the template, I am using the css file as follows
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
and the style.css is present at location DJango_App/static, DJango_App being my project directory with the manage.py file. Still I am getting error
"GET /static/style.css HTTP/1.1" 404 1653
DEBUG is set to True
Directory structure is:
DJango_App
|->DJango_App
|->(settings.py, urls.py, views.py, etc)
|->templates
|->(html templates)
|->static
|->style.css
How do I resolve this?
Update:
Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS, see:
https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Original:
Looks like you did not add the static url handler to urlpatterns.
Serving static files during development require add to urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

static files arent working in django, but i have the same settings as another project I have done in the past?

DEVELOPMENT SERVER - Can't get my static files to work(CSS), I am not sure what I'm doing wrong but I cant seem to get them to load at all.
my HTML template
{% load static %}
<link type="stylesheet" href="{% static 'main.css' %}" />
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [
"C:/Users/Sam/Documents/Django/Blog/OrapiApplied/static"
]
My file layout
/letsdoit
manage.py
/app
/static
main.css
/letsdoit
settings.py
The setting is STATICFILES_DIRS, with an S.
If you add "django.contrib.staticfiles" to your INSTALLED_APPS setting in settings.py, the Django runserver development server will automatically look for all static directories in all your apps.
If you also have static files in other locations (e.g. if you were to add a static directory inside /letsdoit/letsdoit/ which is not an app), you'd have to add it to STATICFILES_DIRS so that Django also finds those.
Note that it's good pratice to namespace your static files for each app:
/app
/static
/app
image.jpeg
So that way you can add {% static 'app/image.jpeg' %} in your template and for a different app you'd be able to also have a image.jpeg which wouldn't conflict with the one in app: {% static 'other_app/image.jpeg' %}.
Add this lines to settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
and don't forget to clear cached images and data of browser before loading the page. Sometimes the browser just keeps giving the browser CSS from the cache.

django won't load staticfiles from statifiles_dirs

My style.css is placed in appname/static/appname/.
My settings.py has this code:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
And in my base.html I load it like this:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
But the styles are not loading.
If I remove STATICFILES_DIRS and change STATIC_URL = '/static/' to STATIC_URL = '/static/appname/', it works perfectly, but I guess it's not the best practice for the case I'll add any other app to the project later. What I might be doing wrong?
Just change one thing,
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
It will search in static folder inside your app. Also if you want to add a specific directory,
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), '/your specific directory/',
)
From here you can directly add the particular file name, and djnago will search in that specific directory.
Remove "appname" in {% static 'appname/style.css' %}, you must not place it there because python knows automatically in which application the file is, it get the application name from the request
By default django picks static directory from app's directory. So, if your static directory is inside app directory there is no need to specify STATICFILES_DIRS.
Now /static/ will point to files and directories in the static directory of your app. To refer your css file use
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">

STATICFILES_DIR and STATIC_URL for modular templates

I am learning Django and trying to create modular templates and I am coming across this issue
In my Developer tools I am getting this error:
GET http://127.0.0.1:8000/static/assets/css/default.css 404 (NOT FOUND) 127.0.0.1/:8
GET http://127.0.0.1:8000/static/assets/images/pythonlogo.jpeg 404 (NOT FOUND) 127.0.0.1/:84
From my understanding, having STATIC_URL = '/static/' allows {% static %} to be used and also appends /static/ to the path of your static folder. Also, using STATICFILES_DIR is for locating the static files in your project.
Currently I have:
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/Users/BobDole/Development/django-brad/django_test/'),
)
From reading the documentation, it seems to me that 'assets' is used as a namespace or a variable to represent /Users/BobDole/Development/django-brad/django_test/
In my html page I used
<img src="{% static 'assets/images/pythonlogo.jpeg' %}">
<link rel="stylesheet" type="text/css" href="{% static 'assets/css/default.css' %}">
My current project directory structure
django_test/
admin/
article/ <-- app
templates/
django_test/
templates/
images/
static/
css/
I believe that I am using STATIC_URL and STATICFILES_DIR improperly could some provide me with some suggestions? Thank you!
try this:
<img src="{{ STATIC_URL }}assets/images/pythonlogo.jpeg">
Django will change STATIC_URL for /whatever/whatever/static/, so the url the img will access is: /whatever/whatever/static/assets/images/pythonlogo.jpeg
The thing is /whatever/whatever/static/ has to be the path until the "static" folder inclusive
I use in my projects STATIC_URL like this(settings.py):
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'
EDIT You have to add this 3 lines in your settings.py
What makes you think assets is magic in some way? It's not, it's simply the name of a directory. You don't have a directory called that, so you shouldn't use it. Use {% static 'images/pythonlogo.jpeg' %} etc.

Categories