【问题标题】:Django: Question on Template InheritanceDjango:关于模板继承的问题
【发布时间】:2011-05-16 11:04:14
【问题描述】:

您好,我似乎在使用基本模板时遇到了问题。我的基本 html 称为 help_content.html。

<html>
<head>
    <meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8">
    <a href="help_new_client.html">New Client</a>
    <title>User Manual</title>
    <style></style></head>
<body style="padding:10px;">
    {% block content %}{% endblock %}
</body>
</html>

这是我的名为 help_new_client.html 的子模板

{% extends "help_content.html" %}
{% block content %}
<h3 class="western">New  Client</h3>
<p><b>Add client</b></p>
<p>If you are not already on the All clients screen then click “VIEW
CLIENTS” on the main menu.</p>
<p>Click on the Add client button. A Client form is displayed. Fill
the form and click save.</p>
<p>Action: VIEW CLIENTS → Add client → save</p>
<p><b>Edit client</b></p>
<p>To edit a  client simply click on the client in the All clients
list. Edit the clients information and save.</p>
<p>Action: VIEW CLIENT → click on client → click on Edit client
information → save 
</p>

{% endblock %}

编辑:观点

@login_required
def help_index(request):
    return render_to_response('help_content.html', context_instance=RequestContext(request))

@login_required
def help_new_client(request):
    return render_to_response('help_new_client.html', context_instance=RequestContext(request))

我不确定我做错了什么。在 help_content.html 中,我看到了 {% block content %}{% endblock %},在 help_new_client.html 中,我看到了 {% extends "help_content.html" %} {% block content %} {% endblock %}。我不确定为什么我会收到这些模板标签而不是我的内容。

【问题讨论】:

    标签: python html django templates


    【解决方案1】:

    我猜你不是从视图中渲染模板。

    您确定您正在视图中执行类似的操作,并且您正在通过相应的 url 模式执行该视图:

    from django.template import RequestContext
    from django.shortcuts import render_to_response
    def index(request):
        return render_to_response('help_content.html',
                          context_instance=RequestContext(request))
    

    我没有在您的扩展模板中看到这是第一行:

    {% extends 'help_content.html' %}
    

    并关闭基本模板中的&lt;/body&gt; 标记以确保安全。

    【讨论】:

    • 由于某种原因仍然无法正常工作。我为基本模板和子模板创建了一个视图,并将它们的链接放在 urls.py 文件中。所以还是和以前一样。
    • 我的模板中有{% extends "help_content.html" %}。我已经删除了已编辑的正文标签,但仍然存在问题。
    • @Shehzad009 你真的确定 Django 正在解析模板而不是服务器返回它找到的文件
    • 好吧,我想我可能已经做到了。最初 help_new_client.html 和 help_content.html 都在我的媒体目录中。我已将它们都移动到主模板目录 - 并调整了它们的 url 路径。
    【解决方案2】:

    我在模板文件夹中有我的基本模板。我有所有其他模板的子文件夹。

    view.py

    from django.shortcuts import render_to_response;
    def help_index(request):
        return render_to_response('html-template/help_new_client.html')
    

    在你的情况下是

    return render_to_response('help_new_client.html')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多