How to link a different style sheet from the base template style sheet in django

2

Let's say that we have the following template as the basis of a project in django (Here it will be called base.html ):

{% load staticfiles %}

<html lan="pt-br">
  <head>     
    <link rel="stylesheet" type="text/css" href="{% static 'base/style.css' %}" />
  </head>
  <body>
    {% block content %}
    {% endblock %}              
  </body>
</html>

This templante has a css style sheet in its head that contains the basic settings of the template.

Below is an example template that inherits from the basic template (called form.html ):

{% extends 'base.html' %}
{% load staticfiles %}

    {% block content %}

    <form class="form" method="post">
      {% csrf_token %}
      {{ form }} 
      <button type="submit" >Cadastrar</button>
    </form>

    {% endblock content %}

Since the form.html template is a block in the base.html body and has no head, how can you bind a unique style sheet to the components of form.html and still maintain the compatibility of the previous style sheet?

    
asked by anonymous 22.08.2017 / 23:57

1 answer

2

First in your base.html create a new block inside the head tag:

<head>
        <link rel="stylesheet" type="text/css"
            href="{% static "appfolder/css/base.css" %}" />
        {% block novobloco %}{% endblock %}
    </head>

Now, call it inside your template, passing the file to the block:

{% extends "base.html" %}
    {% load static %}

    {% block novobloco %}
    <link rel="stylesheet" type="text/css"
        href="{% static "arquivo.css" %}" />
    {% endblock %}

    {% block content %}
      main
    {% endblock %}
    
23.08.2017 / 17:02