【问题标题】:Why wont my {% extends %} command work in my django app为什么我的 {% extends %} 命令不能在我的 django 应用程序中工作
【发布时间】:2016-08-04 04:38:42
【问题描述】:

我正在使用 django 制作一个网络应用程序,并且在一部分中我试图利用 {% extends %} 命令将一些 html 从一个模板放到另一个模板上。代码如下:

home.html -

<!doctype html>
<html>
<head>
</head>
<body>
 {% block content %}
 {% endblock %}
</body>
</html

search.html -

{% extends "gamelobby/home.html" %}
{% block content %}
 <h1>Hello World</h1>
{% endblock %}

知道可能是什么问题吗?

home.html 视图的代码 -

def index(request):
    all_games = GameCard.objects.all()
    template = loader.get_template('gamelobby/home.html')
    context = {
        'all_games': all_games,
    }
    return HttpResponse(template.render(context, request))

【问题讨论】:

  • 你还没有告诉我们什么不起作用?你有错误吗?
  • 我猜测一下,你在视图中仍然引用home.html...请尝试创建一个minimal reproducible example
  • @solarissmoke 不,我没有收到错误,它只是没有显示来自 search.html 的 html
  • 向我们展示您视图中调用模板的代码。
  • @solarissmoke 那里我添加了 home.html 的视图代码

标签: python django


【解决方案1】:

您希望将人们引导至您的搜索 view,以便视图必须了解 search.html

def index(request):
    all_games = GameCard.objects.all()
    template = loader.get_template('search.html') <!-- or whichever file -->
    context = {
        'all_games': all_games,
    }
    return HttpResponse(template.render(context, request))

当这个视图加载模板时,它会从gamelobby/home.html 中看到这个extends 并将其拉到周围 块标签中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多