Bringing app templates and static files into Django templates

I am trying to include my gallery.html and gallery.css files within my base.html. After running the collectstatic and runserver commands, I am unable to locate the gallery.css and html files when inspecting with Chrome Dev Tools. Only the base.html and its associated css file are showing up.

This is the project structure:

/proj
   /gallery
      /static
         /css
            gallery.css
      /templates
         gallery.html
   /proj
   /templates
      base.html
   /static
      /css
         base.css

Contents of base.html:

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
    <!-- Base css -->
    <link rel="stylesheet" type="text/css" href="{% static "css/base.css" %}">

  </head>
  <body class="background">
    <div class="wrapper">   
      {% block content %}
        <!-- Content goes here-->
      {% endblock %}
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
  </body>
</html>

Contents of gallery.html:

{% extends "base.html" %}


{% block content %}

<!-- Container for photos-->
<script type="text/javascript" src="{% static "js/masonry.pkgd.minjs" %}"></script>
<link rel="stylesheet" type="text/css" href="{% static "css/gallery.css" %}">
<div class="gallery-container">
    dsfsdfdsfdsf
</div>

{% endblock %}

Contents of gallery.css:

.gallery-container {
    background-color: red;
    width: 50%;
    height: 50%;
    margin: 0px;
    padding: 0px;
}

Contents of base.css:

html, body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;

}
.background {
    background-color:#b0a0e6;
}

.wrapper {
    background-color: grey;
    height: 100%;
    width: 75%;
    margin-left: auto;
    margin-right: auto;
    overflow:auto;
}

settings.py configuration:

STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_STORAGE = (
    'django.contrib.staticfiles.storage.StaticFilesStorage'
)

STATIC_ROOT = os.path.realpath(os.path.join(BASE_DIR, '..', 'websiteAssets'))

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

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
    os.path.join(BASE_DIR,'home/templates'),
    os.path.join(BASE_DIR,'gallery/templates'),
    os.path.join(BASE_DIR,'contact/templates'),
    os.path.join(BASE_DIR,'aboutme/templates'),
)

Answer №1

When you use the code line {% static 'css/gallery.css' %}, you are attempting to access the static file gallery.css from the directory 'css'. However, this directory does not exist as the gallery file is only placed directly in the 'static' directory and not within a subdirectory like 'static/css'. To fix this issue, consider using {% static 'gallery.css' %} instead or create a 'css' directory within the 'static' folder and move the gallery.css file into it.

Answer №2

While this question may be old and you may have already found a solution, it's important to note that adding the following code snippet is necessary:

{% load static %}

This should be included in your gallery.html, right below the {% extends 'base.html' %} line. In Django templates, it is essential to load static files in every template where they are used, even if they are included in a template being extended. For more information on this topic, you can refer to this link.

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

What is causing the buttons to be concealed by the leaflet map...and what can be done to make them more visible

I need assistance with positioning responsive buttons over a leaflet map. Currently, the buttons are hidden behind the map and not visible. How can I bring them forward? Here is the map I am referring to: Map Link The desired button appearance should be ...

The layout is being disrupted by the PHP if statement, causing a significant amount of extra space

My table was functioning normally until I added an if statement to restrict editing and deleting posts for those who are not the author. Strangely, this caused a large white space to appear. The if statement only involves the last 2 columns (Edit and Delet ...

To prevent the animation from overflowing, set the parent element to have hidden overflow while still displaying the child element

I am facing an issue with two menus that can be toggled using a switch. Each menu item has a dropdown that appears when clicked. The problem arises when switching between the menus, as there is an animation for the transition (slide in and slide out). I wa ...

Perform a function in jQuery only after the previous one has finished running in a cascading manner

Looking for guidance from an expert on how to accomplish this task. I have HTML code for a multiple choice question with options A to E: <div id="correct-answer-XXXXX" style="display:none;"></div> <div id="wrong-answer-XXXXX" style="display ...

List that scrolls with a stationary button

I am working on an interface that includes a button with a dropdown list. Here is an example: <a href="https://jsfiddle.net/DTcHh/16589/" rel="nofollow">jsfiddle</a> While the list is scrollable, I am looking to add a fixed button at the botto ...

Refreshing the page after executing a MySQL UPDATE statement

There seems to be a problem with the page refreshing faster than the query execution. I have an UPDATE query to update my database with values in my fields, triggered by pushing a button. However, upon clicking the button (which refreshes the website), th ...

Arrange and pile up 3 columns using Bootstrap 4

My project currently has a structure using bootstrap columns, as shown in the image below: https://i.sstatic.net/JufaU.png However, I need to change to a lower resolution and rearrange the columns like this: https://i.sstatic.net/IB9sY.png I came acr ...

Ways to apply Position Fixed to a particular Div element and subsequently eliminate that class while scrolling

Check out my current Jsfiddle project here I am working on a task that involves adding a class to a specific div when scrolling and then removing that class. Below is the JavaScript code I have written for this functionality: var targetDiv = $(".mainwra ...

What is the best way to position a Radio group next to a TextField in Material UI

I've been through the documentation, but I'm struggling to understand how styling works in Material UI. Currently, I have a radio-group component set up like this: import React from 'react' import Radio from '@material-ui/core/Rad ...

HTML5 Device Orientation Emulator

Is there a tool to simulate device orientation for an HTML 5 interpreter (also known as a web browser)? I have been experimenting with the DeviceOrientation Event Specification in HTML 5 and I have successfully coded an example that works on my Android sm ...

Attempting to change the src of a different image using jQuery upon clicking

I have two separate divs that change their background image source when clicked, which is working fine. However, I would like them to change the other image they are paired with. For example, if div 1 is clicked and becomes "open", then if div 2 is "open" ...

What is the best way to direct users to an input field within a dynatree title?

I am currently utilizing Dynatree to display a tree view, and my goal is to focus on an input field within the dynatree title element. However, I am encountering an issue where the focus is being lost. My code attempts to address this problem but unfortun ...

What is the best way to insert a Bootstrap Glyphicon into an asp:Button in ASP.Net?

In my current project, I am utilizing Bootstrap 3 and aiming to incorporate Bootstrap Glyphicons into ASP.net buttons. However, I encountered an issue when using the following code (I discovered a solution that utilizes Twitter Bootstrap <span> tags ...

How can I restrict input to only numbers and one decimal point using JavaScript?

The following code I have is not functioning as expected: function validateInputValue(element) { var regex = /\D$/i; // Important: avoid white spaces in password if(regex.test(element.value)) { alert("Please check your input format"); e ...

What is the best way to pass a bind value in an Angular function controller?

I am new to working with AngularJS and I'm trying to pass a model bind value into a function declared in the controller. However, when I try to access that value from the controller, it returns undefined. Here is the code snippet: HTML: <div> ...

Is there a way to create a function that can show the pathway on the Browser Console?

I am looking to create a function that will show the path in the Browser Console when a link in the menu of a sub-category is clicked. The menu setup resembles this () on an e-commerce website. For instance: Perfume => ForMen => Cologne How can I r ...

Interact with jQuery to trigger events upon clicking on a list item, excluding checkboxes within the list

I'm in the process of developing a straightforward web application. I have a dynamically generated list on one section: Subsequently, I set up an event that triggers when any element within the list is clicked: $(document).on('click', &apo ...

Adjust the class attribute in real-time

My image doesn't appear in the correct position when I dynamically set it using the margin-top. Below is an image with a red arrow pointing to the right, which is what I want: https://i.stack.imgur.com/ue4mY.png The CSS I have is as follows: .file ...

What could be causing the flexbox code to not take effect and the text to refuse wrapping?

I've been utilizing flexbox to style my content, and it worked seamlessly on the first three divs. However, when I attempted to apply the same styling to the fourth one, it didn't quite work out as planned. Despite trying options like flex-wrap t ...

Retrieve information from various Django application models on a unified HTML webpage

I am currently working on a project called Project_Name that includes an app named first_app containing various articles. These article titles are displayed on the home page as clickable links that redirect to the individual article pages on the app. For ...