How to link a different stylesheet to the Django base template stylesheet

Asked

Viewed 113 times

2

Let’s say we have the following template as the basis of a Django project (which will be called here html base.):

{% 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 linked a css style sheet in its head that contains the basic template settings .

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

{% 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 template html form. and a block in the body of html base. and has no head, how is it possible to link a style sheet to the components of html form. and still maintain compatibility of the previous stylesheet ?

1 answer

1


First in his html base. 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 him 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 %}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.