Django Staticfiles not found css does not load

Asked

Viewed 158 times

0

I am developing a Jango application, everything is running perfectly, but the static file is not being rendered on in my Catalog.

I made the settings in Settings but it still doesn’t work. below is how I put the settings in Settings as I pulled the static file in Templete.

Settings in the Settings:

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    'static',
]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

How I’m pulling on the template:

{% load static %}

<!doctype html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="{% static 'style.css' %}">
    <title>{% block title %}{% endblock %}</title>
</head>

This is the error that the terminal returns:

[07/Jan/2021 20:07:37] "GET / HTTP/1.1" 200 576
[WinError 3] O sistema não pode encontrar o caminho especificado: 'C:\\Users\\rodri\\OneDrive\\Documentos\\PYTHON\\DJANGO\\projeto_final\\clientes_admin\\staticfiles/style.css'
[07/Jan/2021 20:07:37] "GET /static/style.css HTTP/1.1" 404 13

2 answers

0


Rodrigo, write the complete path to the folder containing the static file:

STATICFILES_DIRS = [

    "/home/projeto/app/static",

]

and also checks whether INSTALLED_APPS is mentioned 'Django.contrib.staticfiles',

0

In Settings.py try using these settings:

First Step:

DEBUG = True # caso estiver False  o projeto esta em modo de produção 

ALLOWED_HOSTS = [] # caso esteja False adicione na lista ["*"] 

if those directions are Okay!

Second step:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'seuApp,  #adicione a tua aplicação, não o nome do projeto.

In case it’s all right, proceed :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["templates"],# defina essa orientação ao templates.
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

If you are right pass to next indication:

At the end, when a project is created on the last line it looks like this:

STATIC_URL = '/static/' #usado durante o desenvolvimento.
#adicione 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

inside the app create the : Static folder, inside this folder put the css and htmls

in html

{% load static %}


<link rel="stylesheet" href="{% static 'style.css' %}">

the DEBUG variable as False Django will not load html and css files for it to load in production mode will have to use api.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.