A guide on connecting static files in Django when the HTML context includes the file path

Here's the content of my views.py file:

from django.shortcuts import render

def page(request):
    css = 'temp/css.css'
    return render(request, 'temp/index.html', {'css': css})

and this is the content of templates/temp/index.html:

{% load static %}
<!DOCTYPE html>
<html>
    <head>
            <link rel="stylesheet" type="text/css" href="{% static '{{ css|safe }}' %}">
    </head>

    <body>
        Hello Page
    </body>

</html>

now look at the contents of static/temp/css.css:

* {
    width: 100vw;
    height: 100vh;
    background: red;
}

Upon rendering, the source code of the page appears as follows:

<!DOCTYPE html>
<html>
    <head>
            <link rel="stylesheet" type="text/css" href="/static/%7B%7B%20css%7Csafe%20%7D%7D">
    </head>

    <body>
        Hello Page
    </body>

</html>

However, I am expecting it to show:

...
<link rel="stylesheet" type="text/css" href="/static/temp/css.css">
...

Why isn't it working as expected? Is there a way to achieve this? How can I link a static file based on the path provided in the context within HTML?

Answer №1

If the context variable 'css' is what you are working with, you can simplify your code by removing the quotation marks around it. Here's how you can achieve that:

<link rel="stylesheet" href="{% static css %}">

Answer №2

It is recommended to directly include

<link rel="stylesheet" type="text/css" href="/static/styles/main.css">
in your index.html file. This ensures that the stylesheet is automatically applied and eliminates the need to pass it as context.

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

Getting the list items separated from the unordered list in Selenium

My current task involves navigating a list and selecting the correct text entry. The list contains only 2 items: <ul> <li><span>first</span></li> <li><span>second</span></li> </ul> However, ...

Retrieve the value of the textarea only if it exceeds 15 characters

Could you please explain why in this example: <div id="comment-form"> <h3>Comment on this story:</h3> <form method="post" id="story-comment-form" action="#"> <textarea name="comment" id="story-comment-text"></textarea& ...

Blur function not performing as anticipated

I am attempting to achieve a blur effect on a dialog pop-up. Currently, I am using the primeng p-dialog component for this purpose. <p-panelMenu [model]="items" [style]="{'width':'300px'}"></p-panelMenu> <p-dialog head ...

Issue with Jquery addclass not activating upon clicking on li element

My menu structure is as follows: <ul class="menu_bg" id="menu-main-menu"> <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-653" id="menu-item-653"><a href="http://apptivowp.apptivo.com/" title="Home">Home&l ...

Namespace SyndicationElementExtension refers to

I'm trying to achieve the following output: <someNSalias:full-text>Good news everyone!</someNSalias:full-text> This is the code I have written: var element = new SyndicationElementExtension(field.Name, field.HasNamespace ? RssNs : strin ...

Ensure that the div is styled with a white background and positioned next to another div with a right margin

I have a white-colored div that needs to be positioned next to another element on a webpage. Additionally, I need a paragraph placed on top of this div for information purposes. Please refer to the following link to see what I am trying to achieve: https:/ ...

Adjusting the height of content in Angular Material 2 md-tab

I've recently started working with angular material and I'm facing an issue while trying to implement md-tab into my application. The problem is that I can't seem to style my tab content to take up the remaining height of the screen. Could s ...

Django: implementing Facebook login with allauth

My website has a login button that opens the Facebook login in a new window. The issue arises when multiple users try to log in consecutively, as it assumes the previous user is still logged in and does not prompt for new login information. How can I ove ...

Having trouble getting Bootstrap 5 to work with a position-relative row?

I am having trouble placing the * sign on the left side. Whenever I try to do so, it only works if I remove the row class. <!doctype html> <html lang="fa" dir="rtl"> <head> <meta charset="utf-8"> ...

I'm looking to extract information from a webpage using Python by inputting specific criteria

Looking to gather information from a specific website - Link and save it either in a database or another location. The process involves providing input parameters such as entering the vehicle number, submitting it, then verifying if it matches your car. N ...

I encountered an error when working on model managers in Django version 3

After updating to Django version 3, I encountered an error while working on model managers: Error Message: Model Manager is giving error models.py class ProductManager(models.Manager): def get_queryset(self): return ProductQuerySet(self.model ...

What is the method to calculate row height in bootstrap framework?

I have limited knowledge of HTML and CSS, so I am struggling to address an issue on my website. Despite using jQuery and Bootstrap in my template, adjusting the stylesheet has not resolved the problem. The issue seems to be related to the line-height set b ...

JavaScript keeps repeating as you perform basic addition operations

Dealing with a similar issue where things are not adding up correctly, I encountered a problem in a previous question about JavaScript variables only concatenating as strings. The pure JavaScript solution worked fine, so I have now consolidated all the cod ...

How can you create two HTML DIVs side by side with equal heights?

I am working on creating two side by side DIVs, where one contains static content and the other has dynamic content. My goal is to make sure that the height of the static DIV increases whenever the height of the dynamic DIV increases. Here's the code ...

Angular and Bootstrap work hand in hand to provide a seamless user experience, especially

I have been exploring ways to easily close the modal that appears after clicking on an image. My setup involves using bootstrap in conjunction with Angular. <img id="1" data-toggle="modal" data-target="#myModal" src='assets/barrel.jpg' alt=&a ...

Choose all elements following the first child element

My goal is to create a LESS rule that targets every element following the current element, excluding the first child. I've tried using this syntax: (& ~ *):not(:first-child) { margin-left: 5px; } and also this one: & ~ *:not(:first-child ...

An error has occurred in Python Django: TemplateSyntaxError - The remainder could not be parsed

Can you provide a solution to the issue at hand? An error has been identified on line 10. Error during template rendering In template C:\Users\farib\Desktop\Python\classProject\signup\templates\services.html, error ...

Is there an issue with hover not working for MUI styled component when focused?

Having trouble getting both focus and hover effects to work on the same MUI styled component in React. Check out this code sandbox for a demonstration. When clicking on the Select element, the background and border colors do not change: https://i.sstatic. ...

Can you provide guidance on retrieving the record ID after inserting in Django admin?

I want to automatically create a directory with a record ID in Django admin after insertion. How and where can I achieve this? I attempted to do this in the save_model method, but Gallery.ID returns None. Model: class Gallery(models.Model): title = ...

I would like to retrieve my data using my personal API and have them displayed as checkboxes

https://i.sstatic.net/qPSqe.jpgHere is an excerpt of the progress I have made main.html (it's actually a form) <div class="form-group form-check-inline"> <input class="form-check-input" type="radio" name=& ...