8

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:foo@yahoo.com">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?

Danh
  • 5,916
  • 7
  • 30
  • 45
neversaint
  • 60,904
  • 137
  • 310
  • 477
  • The exact same questions was asked here: http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django?rq=1 – Diego Allen Dec 27 '15 at 13:50

3 Answers3

7

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'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Do you still need `settings.configure()` when you're calling `django.setup()`? – approxiblue Dec 21 '15 at 03:51
  • 1
    @approxiblue I am afraid yes, getting `django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.` if `configure()` is not called before..Thanks! – alecxe Dec 21 '15 at 04:04
3

If you are not looking to actually use django models/apps etcetera you might want to look at just using the Jinja template language(what django uses to make templates) if you want to use something else like apache or nginx to serve the output as plain html.

madattarp
  • 87
  • 1
  • 6
  • 4
    Django by default actually uses the Django template language, which Jinja takes inspiration from. Django supports Jinja as well. – approxiblue Dec 21 '15 at 01:58
1

Just to make sure I'm understanding this, are you looking to generate regular html pages based on the django templating engine? If so, I've been using using the django-medusa static site generator. Maybe it's overkill for what you're doing, but I use it to run a local django instance and generic static html that I then upload to various places (godaddy, s3, and more).

It's a little bit of work to set up, but with it you can easily use not just the django templating system but also models, queries, etc.

MBrizzle
  • 1,783
  • 1
  • 18
  • 31