如何在没有服务器的情况下使用Django模板

时间:2015-12-16 00:52:16

标签: python django django-templates

我正在使用Django纯粹用于创建模板(没有服务器)。 这是我的计划:

page1.html

<?php header('Access-Control-Allow-Origin: *'); ?>

base.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 %}

code_to_make_template.py

<!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>

目录结构如下所示:

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)

但是当我跑 current_dir |___ code_to_make_template.py |___ html |_ src |_ templates |_ base.html |_ page1.html 时,我收到了这条消息:

code_to_make_template.py

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

如果你不想实际使用django models / apps etcetera,你可能只想使用Jinja template language(django用来创建模板),如果你想使用像apache或nginx这样的东西来将输出作为普通html提供。