Django isn't serving static files, getting 404 errors

django tutorial
django orm
django models
django queryset
django model fields
django documentation
django database tutorial
django rest framework

I can't seem to get my static files to load from my templates. I've followed the official documentation but I must be missing something.

My directory layout (generated by Django, most files omitted):

myproject
  myproject
    settings.py
    urls.py
  static
    css
      bootstrap.css
      main.css
  templates
    base.html
  myapp1
  myapp2
  ...
  manage.py

My settings.py:

STATIC_URL = 'static/'

I'm referencing my stylesheets like so (from my templates):

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

Which gives this once rendered (in HTML):

<link rel="stylesheet" href="static/css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="static/css/style.css" type="text/css"> 

Yet these links don't actually lead anywhere (when I visit them I get 404 error from Django). I feel that I could fix this by adding something in urls.py, but I thought Django did this automatically when you run the server? What am I missing?

Have you defined your static files directory in settings.py ?

I'm guessing you have 'django.contrib.staticfiles', in your installed apps.

If you haven't defined your static files dir, you could by doing something like this:

import os.path

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

Databases | Django documentation, We ask today—and always—that the Django community comes together in support A model's database table name is constructed by joining the model's “ app  Big Daddy: Django isn't a slave. Django is a free man. You understand? You can't treat him like any of these other niggers around here, cause he ain't like any of these other niggers around here.

This is the working solution for static/media/template access in django for windows,

settings.py

import os.path

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join('static'),
)

Model Meta options | Django documentation, This is for convenience in the Python interactive interpreter, so you can immediately see your results when using the API interactively. len(). A QuerySet is  Django contains a registry of installed applications that stores configuration and provides introspection. It also maintains a list of available models. This registry is called apps and it’s available in django.apps: >>> from django.apps import apps >>> apps.get_app_config('admin').verbose_name 'Administration'

My problem was solved by adding "STATICFILES_DIRS" in settings.py file

STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )

QuerySet API reference | Django documentation, If True , Django will store empty values as NULL in the database. Default is False . Avoid using null on string-based fields such as CharField and  djongo' isn't an available database backend django.core.exceptions.ImproperlyConfigured: 'djongo' isn't an available database backend. Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'

I thought Django did this automatically when you run the server?

Why did you think that? If you've followed the official documentation, you won't have found that. Read what you have to do to serve them in development here.

There's another problem. Your STATIC_URL is a relative link, so browsers add it to the existing page URL. So if you're on page /foo, 'static/css/style.css' evaluates to /foo/static/css/style.css'.

Make sure it either starts with / - ie /static/ - or is a full URL, ie http://myserver.com/static/.

Model field reference | Django documentation, Cache-Control. Each header is only added if it isn't already set. cache_timeout is in seconds. The  Django relies on the staticfiles app to manage static files from across our entire project. The startproject command already comes with a STATIC_URL configuration, which sets the URL we use to reference static files. We'll need to set up STATICFILES_DIRS which defines the location of static files in local development.

Django Utils | Django documentation, If you're new to databases, or you're just interested in trying Django, this is the easiest choice. SQLite is  The thing is, Django isn’t hard or complex at the fundamental level. Yes, it has it’s quirks and, yes, big Django projects can be very complex beasts, but bottom line: Django is a very logically structured framework built on the easiest to learn programming language available (Python).

Writing your first Django app, part 2 | Django documentation, We can no longer remain silent. In silence, we are complicit. To eradicate racism, xenophobia, and all other forms of inequality, we must take action. It is our  Solving Django AttributeError: Manager isn’t accessible via <<models>> instances. Agus Bekti Rohmadi. Follow. May 26

System check framework | Django documentation, In silence, we are complicit. To eradicate racism, xenophobia, and all other forms of inequality, we must take action. It is our responsibility and our honour to use  Django comes with a built-in permissions system. It provides a way to assign permissions to specific users and groups of users. It’s used by the Django admin site, but you’re welcome to use it in your own code. The Django admin site uses permissions as follows:

Writing views | Django documentation, Django's default behavior is to run in autocommit mode. Each query is immediately committed to the database, unless a  I am still new to django and I am having problem with my CSS working. I have followed the direction from the link: Django Static Link tutorial, on handling static files. But it is still not working

Comments
  • Try: python manage.py findstatic --verbosity 2 css/styles.css to see where Django is looking for your static files.
  • Yup I've got django.contrib.staticfiles loaded and I've got STATIC_URL = '/static/' in my settings file.
  • Have you filled in STATICFILES_DIRS with the absolute path to your static folder? Like the answer above said to do.
  • Nope! I added the absolute OS path to my static directory and it worked. Thanks alot.
  • Sweet. If you want to read more about static files app ... docs.djangoproject.com/en/dev/ref/contrib/staticfiles/…
  • I'm running this locally, so I tried localhost/static/, localhost:8000/static and /static/ all with the same result. When I used the localhost method, I simply got an about:blank page from Chrome. When I used /static/, I got another 404.
  • In response to the link you gave, I'm using the runserver method and my files are at STATIC_URL, which (according to the docs) means it should automatic. I also tried importing the module suggested by the link and still nothing.