Utilizing django-compressor for multiple LESS files that incorporate shared files: A guide

In my Django project, I have carefully organized the templates so that each page includes a "common" LESS file along with an additional LESS file for page-specific styles.

The challenge arises when I want the page-specific LESS file to access variables from the "common" LESS file. My initial thought was to create a separate file for variable declarations and import it into both LESS files.

However, due to the structure of Django apps using separate directories for static files, the filesystem ends up looking like this:

common.less and other.less need to import definitions.less. For common.less, the import statement is straightforward:

@import "definitions.less";

Here is how the LESS files are included on the page:

{% load compress %}
{% load static %}

{% compress css %}
    <link href="{% static "css/common.less" %}"
          rel="stylesheet" type="text/less">
{% endcompress %}

What would be the most efficient way to ensure that common variable definitions are accessible to both LESS files? I aim to avoid merging the LESS files for reasons such as maintaining loose-coupling and minimizing data retrieval for individual pages.

  • Removing the ability to deactivate an app without affecting the rest of the site.
  • Increasing the amount of data fetched for a single page by including all styles for all apps.

Answer №1

In our app, we follow a similar approach by using a main.less file that is shared among other less files. Surprisingly, we haven't encountered the same issue as you have mentioned. One possible solution could be to adjust the location of your static files.

Within Django, there is a configuration called STATICFILES_DIRS that allows you to modify the location of your static files instead of organizing them within nested apps. https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATICFILES_DIRS

Our coding structure resembles this:

{% compress css %}
{% include "_base_css.html" %}
{% endcompress %}

Where _base_css.html includes all the less files, with main.less taking precedence.

## _base_css.html
<link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}css/main.less">
<link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}css/about.less">
<link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}css/dashboard.less">

Based on your query, it's not entirely clear what error you're encountering, but it seems essential for your common less file to be included before any other less files to ensure accessibility. From your provided code snippet, it appears that this inclusion might be missing. Regarding compression, consolidating all site-wide CSS into one file may not be too detrimental - it results in a single download initially and then remains cached.

Hoping this information proves useful to you.

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

Achieving a tidy layout with child divs inside a parent div

I'm struggling to figure out how to arrange objects with margins inside a parent div. Initially, I thought using float would do the trick, but I quickly realized it wasn't the solution. This results in an unsightly amount of space. The divs that ...

Tips for customizing MUI PaperProps using styled components

I am trying to customize the width of the menu using styled components in MUI. Initially, I attempted the following: const StyledMenu = styled(Menu)` && { width: 100%; } `; However, this did not have any effect. After further research, I ...

Exploring the connection between two divs using onmouseover and onmouseout events in JavaScript

I am currently working on creating a function that displays a button when the mouse hovers over an element and hides it when the mouse is moved away. The example below illustrates my issue: ------------------------------------------------------------ - ...

When the cursor hovers over the button, it

I'm looking to enhance the interactivity of my buttons so that they change color when clicked, but I am facing issues with the CSS pseudo-class .button:focus not working as expected. Below is the code snippet I am working with: ul { list-style-t ...

Setting the overflow-y property to hidden on an element prevents the scroll reveal animation from activating

Currently, I have implemented scrollreveal.min.js on my WordPress website. Although Scroll reveal is functioning properly, I encountered an issue when trying to hide the vertical scroll bar that appears during animation for a specific section using over ...

Modify the css based on the user's input

<html lang="en"> <head> <link rel="stylesheet" href="style.css" /> <li id="visa"> <section class="credit-card visa gr-visa"> <div class="logo">visa</div> <form> <h2>Payment ...

Using border-spacing on table body rows exclusively

In my table, I have 2 header rows and multiple body rows. To create a spacing of 10 pixels between body rows, I used the following CSS: border-collapse: separate; border-spacing: 10px; However, this also affected the spacing in the header rows, which I w ...

Unable to establish a WebSocket connection in Django channels due to connectivity issues

As I dive into creating a socket-based application using Django-Channels, I've encountered an issue with connecting to the WebSocket. To demonstrate this problem, I set up a test project. The error message displayed in the JS Console: WebSocket con ...

Utilize Django ORM to join a table twice

There are two models that I am working with: class Customer(AbstractBaseModel): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_column='uuid') name = models.CharField(max_length=55) class CustomerAddr ...

Choosing containers as found in the Firefox debugging tool

I'm currently working on developing a container selector similar to the one found in Firefox's debug tools (press ctrlshift+C). The main goal is to retrieve a list of selected elements for further manipulation. Here is my current progress: http ...

Utilizing Javascript to implement a tooltip feature for dynamically inserted text

I recently incorporated a brief jQuery tooltip plugin into my site. It consists of approximately ten lines of code and works smoothly, as demonstrated in this demo. However, I encountered an issue when attempting to add new text that should also trigger t ...

javascript Something went wrong

Trying to integrate bower with Django framework, I have completed the following steps: - Installed bower using sudo. - Installed packages along with their versions: npm: 3.5.2-0ubuntu, nodejs: 4.2.6~dfsg-1, bower: 1.8.2 Below is the code snippet: <!DO ...

Visual Composer now appends "?id=" to the background images of columns and rows, enhancing the customization

I am in the process of improving the performance of a WordPress site that is using the Visual Composer plugin. When checking GTmetrix results, I came across this issue: Ensure resources are served from a consistent URL https://example.com/wp-content/uploa ...

Tips for arranging DIV elements alongside images

I am struggling to align 5 images next to each other in order to create a slider that moves left or right. Despite my attempts using float:left, position:absolute, and display: inline, I cannot seem to make it work. Below is the HTML code I have: <div ...

Incorporating Javascript into a Django template: best practices

Using Django templates, I am interested in embedding a Bokeh plot by using Json output. The Json output is already prepared for querying the database. The plot needs to be displayed in a div with a specific ID. According to the documentation, the followi ...

Ways to enlarge an image while hovering the mouse over it without disrupting the positions of other images (JQuery)

Is there a way to achieve a similar effect to Google Images, where the image pops and comes in front when we mouse over it without affecting the positions of other images? This should only happen when the cursor is still and not moving around randomly. I ...

Experience an enhanced replay of canvas drawings with advanced line smoothing technology

Enter your name on the canvas below, and the code will save the drawing coordinates in an array. However, I am having trouble replaying the drawing using these coordinates. It seems to be drawing too quickly! What would be your approach to resolve this is ...

Is it possible to create a directory containing forms and views, and utilize an __init__.py file similar to how models and tests are used?

I have a question about organizing my views and forms into separate directories, similar to how models are handled. Can you provide guidance on creating a directory structure for views and forms in Django projects, including what should be included in the ...

Why is it not possible for me to choose an element after it has been placed within a grid component?

Struggling to eliminate the blur effect on an image inside a grid element? It seems modifying the properties of an element within a grid class is causing some issues. To simplify, I conducted a basic test: I managed to change the ppp2 class when hovering ...

Aligning a rotated paragraph in the middle of its parent container

Here is my current progress: http://jsfiddle.net/2Zrx7/2/ .events{ height:100px; position: relative; } .tt_username{ position: absolute; top:0px; height: 100%; width: 30px; background: #ccc; text-align: center; } .tt_usern ...