django - 在 Django 模板语言中按名称获取 block

标签 django django-templates

我正在使用 django (1.4) 开发我的网站。 我无法通过其名称获取阻止(以便对其进行编辑)。

在每个继承 self 的基本模板的模板中,我都分配了一个字符串 blockname。我想在一个特定的 block (在扩展模板中定义)中放置一些文本,该 block 的名称等于变量 blockname 的内容。

编辑:

例子

base.html:

{% block b1 %}no changes{% endblock %}
{% block b2 %}no changes{% endblock %}
{% block b3 %}no changes{% endblock %}

subpage.html:

???

用这样的 View 显示 subpage.html:

def show_subpage(request):
    t = loader.get_template('subpage.html')
    c = RequestContext(request, {'blockname': 'b2'})
    return HttpResponse(t.render(c))

应该有如下结果:

no changes CHANGE no changes

我怎样才能做到这一点?

最佳答案

如果我理解正确,那么这就是您想要的...

def show_subpage(request):
    t = loader.get_template('subpage.html')
    c = RequestContext(request, {'blockname': ' Change '})
    return HttpResponse(t.render(c))

subpage.html

{% extends 'base.html' %}

{% block b2%}{{ blockname }}{% endblock %}

关于django - 在 Django 模板语言中按名称获取 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997981/

相关文章:

python - 部署 Django 应用程序

python - Eclipse osx 中的 MySQLdb

python - 如何在 Django 模板中显示多个 View ?

Django 模板 - 更改 'include' 模板的上下文

django - 逐字包含模板

python-3.x - 如何从 Django 模板中的 API 响应转换日期

python - 在 django admin 中按自定义列表显示字段列表过滤器

django - 编写 Django 脚本

python - 使用 python (django) 进行 AWS Elastic Beanstalk 日志记录

python - 类型错误 : Cart() takes no arguments