Unsuccessful CSS integration with Django webpage

I have created a navigation bar using HTML and CSS, but I am having trouble getting the CSS to affect the HTML. I have checked my static files setup and tried various settings, but nothing seems to work. Any assistance would be greatly appreciated. As a beginner in HTML and CSS, any mistakes I may have made are welcome for correction. Thank you!

settings.py

""
Django settings for completestudent project.

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

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 = 'siv&qzku5zi8b!2d=%0@z2i34eje)$-t#ezbdot1-e9^zahgg@'

# 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',


#own apps
'expendetures',
'pages'

]

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 = 'completestudent.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 = 'completestudent.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_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, "pages/static/css")]

navbar.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel = ”stylesheet” type = "text/css" href =  "{% static 'css/navbar.css' %}">
</head>
<div class="topnav">
<a class="active" href="{% url 'home' %}">Home</a>
<a href="{% url 'dashboard' %}">Dashboard</a>
<a href="{% url 'contact' %}">Contact</a>
<a href="{% url 'about' %}">About</a>
</div>
</html>

navbar.css

.topnav{
background-color: #8a2be2;
overflow: hidden;
}

.topnav a {
    float: left;
    color: #ffffff;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.topnav a:hover{
    background-color: #ffffff;
    color: #8a2be2;
}

.topnav a.active {
    background-color: #ffffff;
    color: #8a2be2;
}

Answer №1

To simplify your settings.py file, try excluding STATIC_ROOT and solely utilize STATICFILES_DIRS as demonstrated below:

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

STATIC_URL = '/static/'

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

The documentation on STATIC_ROOT recommends keeping it empty for production convenience. So unless you're deploying to a server, it's generally not necessary.

Answer №2

The navigation directory is crucial for locating CSS files

/project-directory/app/static/styles/navigation.css

After executing python manage.py collectstatic, navigate to

/project-directory/static/app/styles/navigation.css

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Flexbox design that adapts responsively to display 3 items per row, shifting to 2 items and finally

I have a segment with six elements. For desktop, I would like to display 3 items per row; for medium devices, 2 items per row; and for mobile devices, only 1 item per row. <div class="flex-container"> <div>1</div> <div>2</d ...

Tips for aligning the dialog box in the center of the screen based on the current scroll position

Is there a way to center the dialog box vertically at the current scroll position of the window when any of the "show dialog" buttons are clicked? For instance, if I click on location 3, I want the dialog box to be centered vertically within the current v ...

When using Selenium WebDriver's get_attribute method, it may return a truncated value of the href attribute if the value

I've been attempting to extract the href attribute value from an anchor tag on a specific page within my application using Selenium Webdriver (Python), but I'm encountering an issue where part of the result is getting stripped off. Below is the ...

Decode my location and input the address before validating it

While I have come across numerous plugins that enable geolocation and display it on a map, I am searching for something unique. I am interested in implementing geocoding when a user visits the page with an option to "allow geolocation." If the user agrees ...

Connect your Flask/Dash application to a customized Bootstrap stylesheet stored locally

I have created a Flask/Dash application and I am currently linking it to an external bootstrap stylesheet as shown below: dash_app = dash.Dash(server=server, routes_pathname_prefix='/dashapp/', ...

Troubleshooting Bootstrap 4 Scrollspy CSS Selector Problem

As I work on my website, I am attempting to implement a scrollspy with Bootstrap 4. Unfortunately, it seems that the functionality is not working as expected. I suspect that the issue lies within the classes and selectors I have utilized. Interestingly, wh ...

get the value of the last selected element using jQuery

I am facing an issue where I cannot retrieve the selected value from my second select element. Even though I have selected the second option in the dropdown, it keeps returning the value of the first option as empty. Here is a snippet of the HTML code: & ...

Enhancing Data Retrieval in Next.js: Implementing Typed Requests and Responses with the Fetch API

Context I've been developing a web application using Next.js and a custom Django Python backend, but I am struggling to find an efficient approach for making API requests from my frontend to the backend. My main goal is to centralize the logic for fet ...

Determining the appropriate time to establish a new class

As I dive into learning about classes, I find myself grappling with when it's best to create a new class versus using a function. Currently, I have a "Stats" class that retrieves information from a website, stores it in a database, and creates graphs ...

Is it possible to eliminate the css class prefix when using Modular css in React?

When working with React & NextJS, I have observed that my modular SCSS files are automatically prefixing my classnames with the name of the file. Is there a way to turn off this feature as it is making the DOM structure difficult to interpret? For instanc ...

The function of jQuery .click() not triggering on elements within msDropDown

I'm having difficulty implementing jQuery on an Adobe Business Catalyst site. The HTML snippet below shows the structure: <div class="banner-main"> <div class="banner-top"> <section class="banner"> <div class="catProd ...

What is the best method for automating the transfer of data from a database to the user interface of a website using html and javascript?

As a volunteer coordinator for a non-profit organization, I have some basic experience working with Python. I am willing to dedicate fewer than 8 hours of learning time to my current project in order to automate certain tasks (or else I will do them manual ...

I am attempting to display a JSON array in an angular application

How can I display a JSON Array on the UI using AngularJS? I have the JSON array in the controller, and I'm attempting to retrieve it in the view. Below is the code snippet of what I have implemented. Please provide feedback on whether my approach is ...

The authentication system in Django rest-auth functions smoothly with superusers, but encounters issues with non-superuser accounts, resulting in the error message "Unable to log in

Is there a reason why rest-auth is only working correctly with superusers? I'm encountering an error when trying to log in a user without staff or superuser permissions, as shown below. The user gets registered successfully though. { "non_field_e ...

Exploring the advanced functionalities of Django's prefetch_related and select_related in the Admin panel for intricate relationships

My project involves a complex relationship between multiple models, illustrated by a simplified example: class Country(models.Model): name = models.CharField([...]) [...] def __ str__(self): return f'{self.name}' class Regi ...

Enhancing Product Images in Shopify with the Zoom Feature

Exploring the functionalities of the Shopify Timber framework can be an engaging experience. I recently attempted to integrate a zoom option for images on a single product page. While the zoom feature worked well with the main product image, it failed to u ...

Set Blueprint container with a full-width background that fills the entire browser window

I'm currently utilizing the Compass framework in conjunction with the Blueprint framework. Within my setup, I've configured a 24 column layout with a total width of 1000px declared in my base.scss file. The HTML structure appears as follows: & ...

Accessing JSON data through crawling may trigger an "Invalid access" warning

Currently extracting data, I came across this base URL After inspecting chrome's developer tools - Network tab, I found the following URL: international?Adt=1&Chd=0&ECITY1=HKG&ECITY2=ICN&Fa…019.03.13.&TRIP=RT&W ...

I am attempting to link a child object literal with a parent object literal

In a practical sense, I believe I can provide a clearer description of the problem I am encountering. What I am trying to achieve is linking a child entity to its parent. In my current scenario, I have two models: owner and property. An owner can have mult ...

What's the reason for the inability to resize fieldset using both?

Can someone explain why the resize control is not enabled on the fieldset element? The documentation mentions that resize does not work with: Inline elements Block elements that have overflow set to visible fieldset { resize: both; overflow: h ...