Question

I am reading through the docs for using bootstrap3-django for templates. I do not understand the purpose of {% bootstrap_css %}. I looked at the source code of this tag, but I cannot understand what its purpose is?

In my base.html template, I have:

{% load static from staticfiles %}
{% load bootstrap3 %}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>{% block title %}{% endblock %}</title>
    {% bootstrap_css %}
    <link href="{% static "css/custom.css" %}" rel="stylesheet">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <!-- Optional theme -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  </head>

<body>
          {% block content %}
          {% endblock %}


</body>
</html>

Here I have {% bootstrap_css %} and css links to the bootstrap css from its homepage

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        <!-- Optional theme -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">

without this, the page is not using Bootstrap3. so what is the purpose of {% bootstrap_css %} and why to use it?

Thanks

Was it helpful?

Solution

Sometimes it's best to dig into the source. the bootstrap_css templatetag code can be found here. Looks like it embeds the bootstrap.min.css and potentially a theme url into the page.

@register.simple_tag
def bootstrap_css():
    """
    Return HTML for Bootstrap CSS
    Adjust url in settings. If no url is returned, we don't want this statement to return any HTML.
    This is intended behavior.

    Default value: ``FIXTHIS``

    This value is configurable, see Settings section

    **Tag name**::

        bootstrap_css

    **usage**::

        {% bootstrap_css %}

    **example**::

        {% bootstrap_css %}
    """
    urls = [url for url in [bootstrap_css_url(), bootstrap_theme_url()] if url]
    return ''.join([render_link_tag(url, media='screen') for url in urls])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top