saturnusringar
saturnusringar

Reputation: 169

Django can't find the static files

I just started a new project and currently Django can't find the static files. I'm using Django==2.2.6

The static files are located in an app called "website". This is the file structure.

https://i.imgur.com/AnPACop.png

This is from the settings:

STATIC_URL = '/static/'

This is how i include the static file:

{% static 'css/style.css' %}

The URL to the static file seems correct:

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

EDIT: its NOT correct. But this works:

<link href="/static/core/css/style.css" rel="stylesheet">

Upvotes: 1

Views: 2916

Answers (4)

Prabhat Singh
Prabhat Singh

Reputation: 1555

Make your file structure like the following one:

ProjectFolderName
   static
      - css
      - js
   template
   website
   projectfoldername
   migrations

Put your static folder in your project folder. Then make these changes to your settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')

Then run this command:

python manage.py collectstatic

You static file will be copied to New file created by django as assets. and add to your HTML

{% load static %}

Upvotes: 3

Bou
Bou

Reputation: 19

My english is not perfect, sorry in advance.

I'm in Django 3 and I had the same problem. This how I find what's wrong, with the help of everyone up me. Just consider this post like a note for me.

I do :

python3 manage.py runserver

At this moment I read the last line of the output. It was looking in a file that did'nt exist. I copied the path. Go in terminal and :

cd path/copied/before/static/base.css

File not found. At this moment I know what to do. Just follow the path and create the folder I need.

I know it's not a good practise but it's can help beginner.

Upvotes: 0

saturnusringar
saturnusringar

Reputation: 169

It's solved. The problem was the file structure. For some reason the static files was in a core-folder. https://i.imgur.com/AnPACop.png

When i put the files directly in "static" it started working.

Upvotes: 0

dstrants
dstrants

Reputation: 7705

This is the URL that the browser will find your static files. It won't let Django know in which folder to find them inside your project root (`BASE_DIR)

STATIC_URL = '/static/'

Try using this instead to specify the directory you are storing the statics

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

Also, make sure you are loading the statics in your template with the following template tag

{% load static %}

Update

The path to the CSS is also wrong on the html you should change it to:

<link href="/static/core/css/style.css" rel="stylesheet">

Upvotes: 1

Related Questions