3

Hi there I'm extremely new to programming/python/django in general and I'm following the django for beginners book and the part where we add in CSS has got me super confused because I'm getting this error "GET /static/css/base.css HTTP/1.1" 404 1660. I've copied the code from the book 1 to 1 and can't get it to work.

Here's the relevant settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

Here's the base.html I'm adding the css to

{% load static %}
<html>
    <head>
        <title>Django blog</title>
        <link href="{% static 'css/base.css' %}" rel="stylesheet">
    </head>

...

And here's the directory info I saw people asking about this in other questions

C:.
├───blog
│   └───migrations
├───blog_project
├───static
│   └───css
└───templates
Nathan J
  • 31
  • 1
  • 1
  • 2

5 Answers5

3

Try this:

Move the static folder from the project root into the particular App directory. Do that for all the apps you create as Django uses django.contrib.staticfiles which takes care of all static files in your project. As referenced here

Asiak
  • 41
  • 4
2

make sure you have the file base.css in the css folder then for some weird reasons try using double-quotes.

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
Ralf
  • 16,086
  • 4
  • 44
  • 68
2

In Django, there is one root static directory which lives under the project directory (at the same level of apps directories),in addition to this, there are other static directories live under Apps directories, and for each app which needs to use static files, A static directory should be created under it to host its particular static staff, so make sure to create static directory under each app so its views and templates can access them and run collectstatic whenever new static staff is added to static directories.

make sure in Setting file that STATIC_DIRC = [ "BASE-DIR /[App-Name].static"] for each app has static directory, dont include the root static directory otherwise Django will throws an error

I have faced this issue and spent few hours looking for solution which I finally got and the credit goes to Asiak answer (mentioned above)

Husam Alhwadi
  • 383
  • 3
  • 11
0

I had the same issue for a while. As you have explained, your code is alright and files are placed in the right directories. However, what I found out to be my issue was not including the following in the app/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)

For a better explanation check out the documentation django-staticfiles documentation

0

close The Sever And Run it again

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 23 '23 at 14:26