Reputation: 63984
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"> © 2015 ·
</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?
Upvotes: 8
Views: 959
Reputation: 1813
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.
Upvotes: 1
Reputation: 473813
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'
Upvotes: 7
Reputation: 87
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.
Upvotes: 3