gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:53:42 31 4
gpt4 key购买 nike

我使用 Django 纯粹是为了创建模板(没有服务器)。这是我的方案:

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)

目录结构如下:

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

但是当我运行 code_to_make_template.py 时,我收到了这条消息:

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

正确的做法是什么?

最佳答案

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

根据Standalone scripts文档段落,您只需要设置 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'

关于python - 如何在没有服务器的情况下使用 Django 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302205/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com