Django 1.6.5 - 404 error with static files - django

I know there are other questions similar to mine, but I've tried everything to get my static files working and need some help. When I open my page I get these errors:
GET /static/css/cerulean/css HTTP/1.1" 404 2983
GET /static/js/bootstrap.js HTTP/1.1 404 2980
This is my project directory (most files ommitted):
Motif_Django
Motif_Django
settings.py
urls.py
motif
static
css
cerulean.css
js
bootstrap.js
Here are the important settings in settings.py:
STATIC_URL = 'http://127.0.0.1:8000/static/'
STATIC_ROOT = '/home/kimberly/Motif-Scan-Plus/Motif_Django/static'
STATICFILES_DIRS = (
'/home/kimberly/Motif-Scan-Plus/Motif_Django/static',
'/home/kimberly/Motif-Scan-Plus/Motif_Django/motif/static/motif/',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)
Under INSTALLED_APPS I have included django.contrib.staticfiles. The STATIC_ROOT folder was initially empty and I collected static files with python manage.py collectstatic and I still get these errors. In my HTML form I am using {% load staticfiles %} and trying to load it with
<link rel="stylesheet" type="text/css" href="{% static 'css/cerulean.css' %} but nothing is working. I included the full URL in STATIC_URL to see if that would help, as well as adding the full path of my static folder in STATICFILES_DIRS but still nothing. Any help would be greatly appreciated!!

STATIC_URL is not supposed to be absolute URL, just the path (ie. /static/).
settings.DEBUG has to be True
You have to serve static() url patterns in urls.py
.
urlpatterns = [
# ... patterns,
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Check related guide in Django docs:
https://docs.djangoproject.com/en/dev/howto/static-files/

Related

django-compressor serves old version of CSS file eventhough newer version is available

I am hosting my Django application on a DigitalOcean droplet (Ubuntu 22.10 with Gunicorn and Nginx. When I run my app locally everything looks fine, but as soon as I deploy, it is trying to load the initial version of the compressed CSS file. The newer file lies correctly on the server.
Key files:
Settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
DEBUG = False
urls.py
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
_base.html
<head>
{% compress css %}
<link rel="stylesheet" href="{% static 'src/output.css' %}">
{% endcompress %}
...
</head>
Other static files and media files work fine, but if you open the website, Django serves an old version of the compressed output.css file.
see the live example here: https://gymtime.ch/
the template is trying to load https://gymtime.ch/static/CACHE/css/output.41dd2db47b39.css (404, does not exist anymore)
but the correct file that is used locally is on the server too: https://gymtime.ch/static/CACHE/css/output.e8979ce60e01.css
How can I ensure the template serves the current compressed CSS file?
Thanks for your support!

"GET /css/styles.css HTTP/1.1" 404 2578

I'm new to Django and this is my first time using it. I keep getting the error "GET /css/styles.css HTTP/1.1" 404 2578" I've already looked through other solutions posted and none have helped. I've followed the documentation exactly as laid out by Django's documentation, but it still isn't working.
Relevant settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIR = [
os.path.join(str(BASE_DIR.joinpath('static')),)
]
index.html
{% load static %}
<html lang="en"><head>
...
<link href="{% static 'css/styles.css' %}" rel="stylesheet"/>
</head>...
Relevant urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App1.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
EDIT: It was a simple fix. I simply forgot to add an "S" at the end of STATICFILES_DIR. It is supposed to be STATICFILES_DIRS.
The problem is probably that your index.html file is in a templates folder and not your main Project1 folder. The code is searching for a file with the path templates/static/css/styles.css, which doesn't exist (404 error). Try moving your index.html file outside of your templates folder and see if it works.

Django - static files are not found in root static folder, but settings point to it

I'm starting to configure my first Django project and I find this issue which is really bothering me.
I have set a root static folder to put some css files for my base template there, but Django is not finding any css files there.
My settings.py are like this:
...
BASE_DIR = Path(__file__).resolve().parent.parent
...
STATIC_URL = '/static/'
SATICFILES_DIRS = [
BASE_DIR / 'static',
BASE_DIR / 'sales' / 'static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
...
...
And in my urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('sales.urls', namespace='sales')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT, show_indexes=True)
...
If a run findstatic I get the following:
$ python manage.py findstatic --verbosity 2 static
No matching file found for 'static'.
Looking in the following locations:
/home/dancab/git/django-3-course/myenv/lib/python3.9/site-packages/django/contrib/admin/static
/home/dancab/git/django-3-course/src/sales/static
And also, in the browser, I can see the list of files in the MEDIA folder, but I can't see the STATIC folder, I get the following error:
I don't understand why I Django finds the MEDIA folder and not the STATIC folder.
Thanks in advance to anyone that gives me a hint on why this happens.
before the line STATIC_URL = '/static/', set static root like
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'project_folder/static') # if not works, set actual path
]
don't forget to place your project folder name.
Then run
python manage.py collectstatic
Then you can load static using jinja expression in your html files like
{% load static %}
Then you can link your static css files in root->static->css folder like
<link rel="stylesheet" href="{% static 'css/your.css' %}">

linking css stylesheet django python3?

I am pretty new to Django and I do not know how to link CSS and or javascript to my project.
Don't want to have a project with no CSS. Thank you for any help, sorry if this is a very dumb question. Have a nice day!
you problem is that you need to link static files from your html template.
this is fairly easy in django, just create a folder called staticfiles in the root directory and create a folder called css inside of it. Put your styles in there.
go to your settings.py and change
STATIC_URL = whatever
STATIC_ROOT = whatever
to
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "staticfiles")
]
you see, in django settings, you can't have the static root be the same as what folder you are using for static files in production.
for your urls.py in the folder inside the base directory named after your project
add this after urlpatterns
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += (static(settings.STATIC_URL, document_root= os.path.join(settings.BASE_DIR, "staticfiles")))
now in your base template
put
{% load static %}
before the html tag and
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
in the head tag. I hope this has helped!

How to setup static files in Django [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to organize and load CSS within Django project/app?
I have follow several websites about how to setup static files in Django. Here are my steps.
Configure the setting.py:
STATIC_ROOT = '/Users/kin/mysite/static'
STATIC_URL = '/static/'
Run the collectstatic command:
python manage.py collectstatic
After that, I saw my image file is copied to the STATIC_ROOT.
Then in my template, I try to use my image file by:
<img border="0" src="{{ STATIC_URL }}images.jpg" width="304" height="228">
But there is no image when I load it on the browser. I checked the page source and STATIC_URL seems empty.
Can anyone shed some light here?
Thanks
Dont hard code the path in your setting. I usually put my static files in my main project, so my setting file look like this
import os
MAIN_PROJECT = os.path.dirname(__file__)
then
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(MAIN_PROJECT, 'static/'),
)
After that, you can use {{ STATIC_URL }} in your view
Actually I got it right by just using the following code:
{% load staticfiles %}
<img border="0" src="{% static 'image.jpg' %}" width="304" height="228">
This will render the STATIC_URL path to me.
Thanks all for helping me out!
If you're running your app with python manage.py runserver then you may need to add the following to the end of your url conf (see docs).
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
The process for serving static files with django's dev server is different to the process for doing it with a production web server.
you can try with this
import settings in urls.py
and paste the below code in your urls.py
if settings.DEBUG:
urlpatterns += patterns('django.views.static',
(r'^static_media/(?P<path>.*)$',
'serve', {
'document_root': 'path/to/your/static/folder',
'show_indexes': True }),)
now go to your settings.py and change the setting like
MEDIA_URL = '/static_media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
now access your static file like
<link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}css/header.css" />