0

I know this subject has been discussed before, but ive seen every tutorial and form in stack overflow but cant seem to understand why i cant link my CSS to my HTML file.

This is my directory:
enter image description here

There are 2 apps, main & settings.

For the app named main, i was able to link the html file in from views.py.

I am now trying to link css & js to it.

I created a static file within the base directory that contains 3 folders, css,js,image. enter image description here I believe to have taken all the correct steps in order to connect CSS to HTML, but nothing happens.

I believe that the mistake is with the reference in the HTML file:

<link rel=”stylesheet” href=”{% static 'css/style_login.css' %}”>

But from what ive seen, it seems to be correct.

Bellow ive shared the settings, views, and HTML File.

Settings.py:

"""
Django settings for auditor project.

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

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

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

from pathlib import Path
import os

PROJECT_DIR = os.path.dirname(__file__)

# 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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-$xb)9by$i@f+m3mnt$q&f%to&vjx)jgn#8(h!#(ymf2n)_09%_'

# 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',
    'main.apps.MainConfig', # To tell Django that there is another 
    'settings',
]

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 = 'auditor.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',
            ],
        },
    },
]

WSGI_APPLICATION = 'auditor.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/3.2/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.2/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.2/howto/static-files/


# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),'media')
DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240 # higher than the count of fields





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



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

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm

def index(response):
    return render(response,"main/home.html",{})

def registerPage(request):
    
    form = UserCreationForm()

    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()




    context = {'form':form}
    return render(request,'main/register.html',context)
 

def loginPage(request):
    context = {}
    return render(request,'main/login.html',context)

#def login(response):
#    return render(response,"main/login.html",{})  



# Create your views here.

HTML:

{% load static %}
<!DOCTYPE html>
<html lang="en">


<head>
    <link rel=”stylesheet” href=”{% static 'css/style_login.css' %}”>
    <meta charset="UTF-8" />

</head>

<body>
    <div class="form">
      
        <ul class="tab-group">
          <li class="tab active"><a href="#signup">Sign Up</a></li>
          <li class="tab"><a href="#login">Log In</a></li>
        </ul>
        
        <div class="tab-content">
          <div id="signup">   
            <h1>Sign Up for Free</h1>
            
            <form action="/" method="post">
            
            <div class="top-row">
              <div class="field-wrap">
                <label>
                  First Name<span class="req">*</span>
                </label>
                <input type="text" required autocomplete="off" />
              </div>
          
              <div class="field-wrap">
                <label>
                  Last Name<span class="req">*</span>
                </label>
                <input type="text"required autocomplete="off"/>
              </div>
            </div>
    
            <div class="field-wrap">
              <label>
                Email Address<span class="req">*</span>
              </label>
              <input type="email"required autocomplete="off"/>
            </div>
            
            <div class="field-wrap">
              <label>
                Set A Password<span class="req">*</span>
              </label>
              <input type="password"required autocomplete="off"/>
            </div>
            
            <button type="submit" class="button button-block"/>Get Started</button>
            
            </form>
    
          </div>
          
          <div id="login">   
            <h1>Welcome Back!</h1>
            
            <form action="/" method="post">
            
              <div class="field-wrap">
              <label>
                Email Address<span class="req">*</span>
              </label>
              <input type="email"required autocomplete="off"/>
            </div>
            
            <div class="field-wrap">
              <label>
                Password<span class="req">*</span>
              </label>
              <input type="password"required autocomplete="off"/>
            </div>
            
            <p class="forgot"><a href="#">Forgot Password?</a></p>
            
            <button class="button button-block"/>Log In</button>
            
            </form>
    
          </div>
          
        </div><!-- tab-content -->
        
    </div> <!-- /form -->


</body>


</html>
5
  • Have you tried using only STATIC_ROOT=os.path.join(PROJECT_DIR, 'auditor/static') and/or STATICFILES_DIRS = (os.path.join(PROJECT_DIR, 'auditor/static'),)? Because for me, using only STATICFILES_DIR worked in my recent project Commented Jul 22, 2021 at 18:15
  • Thanks for the comment! I tried without avail, maybe im referencing to the wrong directory? Commented Jul 23, 2021 at 6:38
  • 1
    I believe your PROJECT_DIR is pointing to the wrong directoy, i.r not the app directory. Maybe replace it with the following code in your settings.py folder - PROJECT_DIR= os.path.dirname(os.path.dirname(os.path.abspath(__file__))) and followed with my previous comment and let me know if it works Commented Jul 23, 2021 at 7:28
  • 1
    THanks!!!!IThat worked! I don't know how to give you rep, i guess you have to post the answer. Commented Jul 23, 2021 at 16:01
  • mark it as the correct answer if it worked for you Commented Jul 24, 2021 at 15:42

1 Answer 1

0

As i have answer in the comments above..

I believe your PROJECT_DIR is pointing to the wrong directoy, i.e not the app directory. Maybe replace it with the following code in your settings.py folder -
PROJECT_DIR= os.path.dirname(os.path.dirname(os.path.abspath(__file__))) and followed with using only STATIC_ROOT=os.path.join(PROJECT_DIR, 'auditor/static') and/or STATICFILES_DIRS = (os.path.join(PROJECT_DIR, 'auditor/static'),)

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

Comments

Your Answer

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