Home > Net >  Randomly changing text in django base template
Randomly changing text in django base template

Time:11-01

I have just started learning Django. Currently I am building project with several apps. I have templates folder at the project level which contains base.html. This file contains Bootstrap5 elements Navbar and Card (quote). At the app level I also have templates folder that contains several html files. All of them extends base.html from project level. I would like to randomly change the text in quote Card, so every time when bowser render template from app level it displays new quote. I have Model named Quotes at the app level and I can use quote=Quotes.objects.all(?).first() to select random entry. But I have no idea how to connect to the base.html. Could you give me a hint how to solve my problem?

<!--templates/base.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- CSS only -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <!-- JavaScript Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA /3y gxIOqMEjwtxJY7qPCqsdltbNJuaOe923 mo//f6V8Qbsw3" crossorigin="anonymous"></script>
    <title>{% block title %}{% endblock title %}</title>
</head>
<body>
    <!--NAVBAR-->
    <nav >
        <div >
          <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span ></span>
          </button>
          <div  id="navbarNav">
            <ul >
              <li >
                <a  aria-current="page" href="#">Home</a>
              </li>
              <li >
                <a  href="{% url 'judokas:home' %}">Treneri</a>
              </li>
              <li >
                <a  href="{% url 'judokas:home' %}">Takmicari</a>
              </li>
              <li >
                <a >Statistika</a>
              </li>
            </ul>
          </div>
        </div>
    </nav>
    <!--Quote-->
    <div >
        <div >
          Quote
        </div>
        <div >
          <blockquote >
            <p>{% block quote %}
              {% endblock %}</p>
            <footer >Jigoro Kano in <cite title="Source Title">https://www.azquotes.com</cite></footer>
          </blockquote>
        </div>
    </div>
    {% block content %}
    {% endblock  %}
</body>
</html>

CodePudding user response:

Context processors are perfect for that job. You can pass a context (like in regular views), but it can be used by any template.

def get_random_quote(request):
    return {
        "random_quote": Quotes.objects.all(?).first(),
    }

Then in template:

{{ random_quote }}

Django DOCS

  • Related