Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Django templating as is without server

I am using Django purely for creating template (no server). Here is the scheme I have:

page1.html

{% extends "base.html" %}
{% block 'body' %}

    <div class="container">
        <img src="./images/{{filename}}" style="padding-top:100px; padding-left:100px" align="center" width="60%" heig
    </div>

{% endblock %}

base.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="../src/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="../src/sticky-footer-navbar.css">
        <link rel="icon" href="../images/favicon.ico">

        <title>MY TITLE</title>

    </head>
    <body>


 <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="../index.html">Adjuvant</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="../index.html">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="mailto:[email protected]">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
    <!-- End navbar -->


        <!--- BEGIN INSERT TEMPLATE FOR OTHER PAGE  HERE-->
        {% block 'body' %}

        {% endblock %}
        <!--- END TEMPLATE FOR OTHER PAGE  HERE-->



    <footer class="footer">
      <div class="container">
        <p class="text-muted"> &copy; 2015 &middot;  
     </p>
      </div>
    </footer>


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="../src/jquery-1.11.0.min.js"><\/script>')</script>
    <script src="../src/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../src/ie10-viewport-bug-workaround.js"></script>




    </body>


</html>

code_to_make_template.py

from django.template import Template, Context, loader
from django.conf import settings
settings.configure()

template  = open("htmls/src/templates/page1.html" ).read()
t = Template(template)
filename = "mypicture.svg"
c = Context({'filename':filename})
output_string = t.render(c)

The directory structure looks like this:

 current_dir
   |___ code_to_make_template.py
   |___ html
         |_ src
             |_ templates
                  |_ base.html
                  |_ page1.html

But when I run code_to_make_template.py I got this message:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

What's the right way to do it?

like image 293
neversaint Avatar asked Dec 16 '15 00:12

neversaint


1 Answers

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

According to the Standalone scripts documentation paragraph, you just need to setup Django:

>>> from django.conf import settings
>>> settings.configure()
>>> 
>>> import django
>>> django.setup()
>>>
>>> from django.template import Template, Context, loader
>>> t = Template("Hello, {{name}}")
>>> c = Context({'name': 'world'})
>>> t.render(c)
u'Hello, world'
like image 180
alecxe Avatar answered Oct 20 '22 00:10

alecxe