Troubleshooting static file problems in Django with skelJS and resolving references to images within CSS

Note: The issue causing this problem is related to skelJS, not Django. Please refer to the answer for more information.

Although I've gone through the Django documentation, searched on Google, and read various StackOverflow posts, I'm still struggling with getting static files to work properly in my project.

When the template is rendered, the static paths are correctly inserted (e.g. {{ STATIC_URL }}css/style.css becomes static/css/style.css). I can access

http://127.0.0.1:8000/static/css/style.css
in my browser without any issues.

While the CSS partially works, the images referenced in the CSS file are not showing up. Interestingly, if I navigate directly to

http://127.0.0.1:8000/static/css/images/bg02.jpg
(the body background image), it displays fine. As an experiment, I ran manage.py collectstatic, which copied all my static files (including css/, js/, and image/) along with the un-Django templated index.html to that directory. Surprisingly, everything rendered correctly when I opened the copied index.html file. Additionally, I tried modifying style.css to adapt the image paths but had no success even after testing these changes by visiting
http://127.0.0.1:8000/static/css/style.css
in the browser.

I would greatly appreciate any assistance or guidance you could provide on resolving this issue.

Parent Template base_home.html

{% load staticfiles %}
...
<script src="{{ STATIC_URL }}js/jquery.min.js"></script>
<script src="{{ STATIC_URL }}js/config.js"></script>
<script src="{{ STATIC_URL }}js/skel.min.js"></script>
<script src="{{ STATIC_URL }}js/skel-panels.min.js"></script>
    <noscript>
        <link rel="stylesheet" href="{{ STATIC_URL }}css/skel-noscript.css" />
        <link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" />
        <link rel="stylesheet" href="{{ STATIC_URL }}css/style-desktop.css" />
    </noscript>
    <!--[if lte IE 9]><link rel="stylesheet" href="{{ STATIC_URL }}css/ie9.css" /><![endif]-->
    <!--[if lte IE 8]><script src="{{ STATIC_URL }}js/html5shiv.js"></script><![endif]-->
...

Child Template home.html (called by view, extends base_home.html)

#This is pretty plain, but I wanted to add/learn the functionality early
{% extends "base_home.html" %}

{% block headline %}Insert catchy headline here.{% endblock %}

Rendered HTML

<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/config.js"></script>
<script src="/static/js/skel.min.js"></script>
<script src="/static/js/skel-panels.min.js"></script>

<noscript>
    <link rel="stylesheet" href="/static/css/skel-noscript.css" />
    <link rel="stylesheet" href="/static/css/style.css" />
    <link rel="stylesheet" href="/static/css/style-desktop.css" />
</noscript>
<!--[if lte IE 9]><link rel="stylesheet" href="/static/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><script src="/static/js/html5shiv.js"></script><![endif]-->

settings.py

...
STATIC_ROOT = 'E:/Projects/sandbox/website/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    'E:/Projects/sandbox/website/resources',
    # This dir contains css/ images/ and js/
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    'django.core.context_processors.static',
)
...

views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from sandbox.context_processors import sitevars


def home(request):
    return render_to_response("home.html", context_instance=RequestContext(request, processors=[sitevars]))

context_processors.py

def sitevars(request):
    return {
        'meta_d': 'Meta description here',
        'meta_k': 'Meta keywords here',
        'title': 'HTML page title here',
        'logo': 'Text that needs to be inserted on pages',
        'copyright': r'HTML/text that needs to be inserted on pages'
    }

style.css

body
{
    background: #D4D9DD url('images/bg02.jpg');
}
...
.check-list li
{
    ...
    background: url('images/icon-checkmark.png') 0px 1.05em no-repeat;
}
...
.bordered-feature-image
{
    ...
    background: #fff url('images/bg04.png');
}
...
.custom-feature-image
{
    ...
    background: #fff url('images/bg05.png');
}
...
.item header
{
    ...
    background: #2b3336 url('images/bg01.jpg');
}

Answer №1

Hey everyone, it turns out my initial assumption was incorrect - this issue was not actually related to Django as I originally thought. My apologies for any confusion.

For those of you who are using skelJS...

Once you have beautified or de-minimized the code, make sure to pay attention to the following section:

newStyleSheet: function (a) {
    var c = document.createElement("link");
    c.rel = "stylesheet";
    c.type = "text/css";
    c.href = a;
    return c
},

Instead of c.href = a;, update it to c.href = "/static/" + a; where "/static/" corresponds to the STATIC_URL defined in your settings.py file.

Answer №2

One effective strategy is to utilize relative paths for your image files within your CSS. By doing so, you can streamline the referencing process based on the location of the CSS file. For example, if you have a background-image declaration in a CSS file located at STATIC_ROOT/css/style.css that needs to link to bg02.jpg in STATIC_ROOT/images/bg02.jpg, you would declare it like this:

background-image: url(../images/bg02.jpg)

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

Creating a visually appealing background image using WordPress and PHP

Currently struggling with adjusting the width of an image that's set as a background in Wordpress. The text is perfectly centered, but the background image remains full size and I want it to match the container size... You can view the image at the t ...

What is the best method for transferring HTML tables to Excel with multiple tabs?

I have created a code that exports an HTML table into an Excel file. However, I now need to export multiple HTML tables into different tabs within a single Excel file. Here is the current code: Currently, when I click "Export To Excel," the Excel file is d ...

Automatically updating the results section while executing SQL queries in PHP

Here is a JavaScript/Ajax code snippet: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready (function () { var updater = se ...

"Code snippets customized for React are not functioning properly in the javascriptreact.json file within Visual Studio Code

I'm looking to incorporate some customized React.js code snippets into my Visual Studio Code setup. I am aware of the two files in VSCode that handle this - javascript.json and javascriptreact.json. Although the snippets work well in javascript.json, ...

When I open my website in the browser, the bootstrap card-deck design is not being displayed correctly

I encountered a strange issue while designing my website. The code I wrote for the Bootstrap card-deck class in codeply.com was working perfectly fine. However, when I copied the same code into my PyCharm code editor and then pasted the file link into my b ...

Minimizing switch-case statement into inactive input field (AngularJS)

While the code statement functions as intended, it may not be considered best practice due to the repetition of variables in each case of the switch statement. I am unsure of how to streamline the code or if there is an alternative to using a switch statem ...

Dividing data in Django API based on distinct identifiers

I am currently developing a Django API. Recently, I used a web crawling program to collect data regarding different places and their reviews. However, I encountered an issue where all the reviews are stored together and spread across all URLs instead of ...

What are the steps for utilizing JSON data retrieved from an ajax response?

After successfully making an ajax request and receiving JSON data, I am struggling with how to use it effectively. The response I am getting looks like this: [{ "id": "5", "reviewID": "2389", "serviceID": "50707", "title": "well d ...

To customize or not to customize?

Lately, there has been a growing trend of people incorporating custom attributes into their HTML tags to add extra data for use in JavaScript code. I'm curious to hear thoughts on the practice of using custom attributes and what alternatives are avai ...

Why am I receiving a null response from my ajax request?

I have a working ajax call to fetch XML data from my REST API. However, when I try to echo the results, JQuery returns null. If I use var_dump on the results instead, JQuery accepts the information but it's not formatted correctly and causes errors. ...

Retrieve the value within the <td> element using Ajax

On my Checkout page, I have a table that displays the products customers want to buy. I am trying to retrieve the values of each <td> and save them into a database. However, I seem to be encountering an issue where the values are not being captured p ...

Troubleshooting problem with Angular's ng-repeat directive in dealing with varying number of child objects

I am currently dealing with tree-structured data where the parent nodes can have an indefinite number of children, and those children can also have an indefinite number of children, creating a deeply nested structure. While I have successfully formatted th ...

Creating a visual representation of data using Google Charts to display a stacked bar chart

I wrote a script to display a stacked Google chart using JSON data stored on the wwwroot directory - <html> <head> <title>DevOps Monitoring Application</title> <link rel="icon" type="image/png" hr ...

What is the process of converting a list into a JavaScript array?

Objective: The aim is to take the tweet provided and pass it to a visualization tool to display it on the user interface. Steps: I currently have a 'List' displayed on my UI. I need to convert this list into a JavaScript array (so that I can u ...

Running a bash command within a Node.js application while having root privileges

There is a slight issue that I'm encountering. I have a nodejs app that needs to execute a command in the OS's bash with root privileges, for example: The command is: echo "$password" | /usr/bin/sudo /usr/bin/abc --key "$username" Below is my c ...

Using JavaScript, create a regular expression with variables that can be used to identify and match a specific section of

Struggling to apply a regex (with 1 variable) to compare against a HTML code page stored as text. The HTML code is separated into an array, with each element representing a snippet like the one below. Each element showcases details of fictional Houses (na ...

How to handle Django views when they do not return a value for a request with multiple parameters?

Lately, I've integrated vanilla Django as a backend for my React frontend. My current challenge involves making a POST request using axios to pass a dictionary of two values to my django view. On the frontend, everything seems in order - valid values, ...

The raycaster doesn't remain centered when I'm updating the camera position

Hey there, I'm facing an issue with Raycasting and haven't been able to solve it yet. I need some help with keeping the Raycaster in the center at all times. Even when I move my camera and look in different directions, I want the Raycaster to rem ...

Tips for positioning a Wordpress navigation bar to the right of a logo

My goal is to position the navigation bar next to the logo in my Wordpress theme using Underscores. I have successfully aligned the primary navigation and the logo as desired with the following CSS: .main-navigation { position: relative; float: ri ...

Is there a way to verify the status of a radio button without using a submit button?

What's the best way to determine if the radio box value is Pearson or euclidean? Here's what I have so far: if ($_SERVER['REQUEST_METHOD'] === 'POST') { if($_POST['radio'] == 'Euclidean'){ ...