azazasovich azazasovich - 1 year ago 70
Python Question

Receiving the 404 error in getting of static django files

Please help to understand what I do wrong.

I have in my :

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'

And in index.html :

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "/css/table.css" %}">

But I still have the error 404 :

"GET /var/cardsite/cardsite/static/css/table.css HTTP/1.1" 404 1696

I have this file :

ls -la /var/cardsite/cardsite/static/css/table.css
-rw-r--r-- 1 root root 77 Sep 25 16:15 /var/cardsite/cardsite/static/css/table.css

So what is going on?

P.S. My project stored on "/var/cardsite" and I want to make static folder on each application, like in example is default application "cardsite"


Answer Source

Read this Django_Docs
You must also have a STATIC_ROOT option set before you can use the static files, heres some help add this to your code:

STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'

# Here you can add all the directories from where you want to use your js, css etc
  # This can be same as the static url
  os.path.join(PROJECT_ROOT, "static"),

  # also any other dir you wanna use

# This is the static root dir from where django uses the files from.
STATIC_ROOT = os.path.join(PROJECT_ROOT, "static_root")

you will also need to specify it in the file, just add the following code to the file:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

after you add this, run the command:

python collectstatic

This will copy all the statics you need to the static root dir.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download