How to use Django templates, as without a server

I use Django exclusively to create a template (no server). Here is my diagram:

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 is as follows:

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

But when I ran code_to_make_template.py , I got this message:

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

What is the right way to do this?

+7
python django django-templates
source share
3 answers

django.core.exceptions.AppRegistryNotReady: applications not yet loaded.

According to the paragraph in the documentation for Stand-alone Scripts, you just need to configure 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' 
+5
source share

If you don't really want to use django models / applications, etc., you can only look at the Jinja template language (which django uses to create templates) if you want to use something else, like apache or nginx, for processing output as plain html.

+3
source share

Just to make sure I understand this, do you want to generate regular html pages based on the django template engine? If so, I use the django-medusa static site generator. Maybe this is too much for what you are doing, but I use it to run a local django instance and general static html, which is then loaded into different places (godaddy, s3, etc.).

A bit about working on customization, but with it you can easily use not only the django template system, but also models, queries, etc.

+1
source share

All Articles