1

I'm trying to develop my first web application using Django, so the site looks like that:

Site without changes

On the other hand, I would like to apply some changes and put it in a CSS file called: cabecalho.css

cabecalho.css file (with the changes) - path: (project/static/css/cabecalho.css):

     <style>
       body {
        font-family: Segoe UI;
       }
       .logo {
        float: left;
       }
       .logo img {
        width: 250px;
        padding: 20px;
       }
       .menu {
        float: right;
        padding: 40px;
        font-size: 15pt;
       }
       .menu a {
        margin-left: 15px;
        margin-right: 15px;
       }
       .bemvindo {
        clear: both;
        padding: 0 20px;
       }

       </style>

These changes make the site look like that:

Site with CSS changes

In my HTML file called cabecalho.html, I tried to import with the line code:

1 try: <link rel="stylesheet" href={% static 'css/cabecalho.css' %}/>

2 try: <link rel="stylesheet" type="text/css" href={% static "css/cabecalho.css" %}/>

3 try: <link rel="stylesheet" type="text/css" href="css/cabecalho.css"> 

In this way, nothing happens!

HTML file (cabecalho.html) - path: (project/templates/django/cabecalho.html):

<!DOCTYPE html>

{% load static %} <!-- Nova linha que carrega os arquivos estáticos -->

<html>

  <head>
    <title>Django Form Exemplo</title>
             <link rel="stylesheet" href={% static 'css/cabecalho.css' %}/>
    </head>

    <body>
     <div class='logo'>
       <img src="{% static "images/logo.png" %}" alt="Logo da Hacked" />
     </div>

     <div class='menu'>
       <a href="/app/sobre/">Sobre</a>
       <a href="/app/sair/">Sair</a>
     </div>

     <div class='bemvindo'>
       <h2>Bem vindo ao nosso Site Django Exemplo!</h2>
     </div>

    </body>

</html>

Obs: If I drop this line and add the CSS content like below, the changes happen.

<!DOCTYPE html>

{% load static %} <!-- Nova linha que carrega os arquivos estáticos -->

<html>

  <head>
    <title>Django Form Exemplo</title>
                          <style>
               body {
                font-family: Segoe UI;
               }
               .logo {
                float: left;
               }
               .logo img {
                width: 250px;
                padding: 20px;
               }
               .menu {
                float: right;
                padding: 40px;
                font-size: 15pt;
               }
               .menu a {
                margin-left: 15px;
                margin-right: 15px;
               }
               .bemvindo {
                clear: both;
                padding: 0 20px;
               }

               </style>

    </head>

    <body>
     <div class='logo'>
       <img src="{% static "images/logo.png" %}" alt="Logo da Hacked" />
     </div>

     <div class='menu'>
       <a href="/app/sobre/">Sobre</a>
       <a href="/app/sair/">Sair</a>
     </div>

     <div class='bemvindo'>
       <h2>Bem vindo ao nosso Site Django Exemplo!</h2>
     </div>

    </body>

</html>

Questions that I've ever tried:

<link rel="stylesheet" type="text/css" href="style.css"> isn't work

import css using 'static' template in Django

More information:

views.py (app/views.py):

from django.shortcuts import render
from django.http import HttpResponse


def index(request):


    
    return render(request, 'django/cabecalho.html')

settings.py (project/settings.py):

"""
Django settings for project project.

Generated by 'django-admin startproject' using Django 3.1.3.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#w1odqj&ebl80i3)@m=y5uq@q+rf!+7bk6!&qt@0h8b1#xfy8g'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

TEMPLATE_DIRS = ('C:/Users/Leonardo/microblog/project/templates',)

WSGI_APPLICATION = 'project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_PATH = os.path.join(BASE_DIR, 'static')  # concatena a pasta static a variavel instanciada base_dir que aponta para a raiz do projeto

STATIC_URL = '/static/' # chamada que terá no browswer para a pasta de arquivos estaticos

STATICFILES_DIRS = (
    STATIC_PATH,
)

Folders:

enter image description here

2
  • How are you serving your application? Commented Dec 9, 2020 at 19:02
  • Daniel, I write a CSS code with some settings to be imported into the HTML file. Then I have a view with the code: def index(request): return render(request, 'django/cabecalho.html') Commented Dec 14, 2020 at 13:46

1 Answer 1

0

you are forgetting the outer dubble quotes of href attribute <link rel="stylesheet" href="{% static 'css/cabecalho.css' %}"/>

Sign up to request clarification or add additional context in comments.

1 Comment

in your browser console any error is showing related to cabecalho.css

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.