Py学习  »  Django

初学者问个有关include的问题

jerry • 10 年前 • 2769 次点击  

sample.htm

<h1>Hello, {{name}}!<h1>

index.html

{% include "sample.html" %}
{% include "sample.html" %}

views.py

def home(request):
    return render(request, "index.html",
        context_instance=RequestContext(request, {"name": "jerry"})

现在的问题是,用户访问home视图对应的URL时,页面上将显示两行jerry,有没有社么办法让index.html中两次不同的include接收不同的参数呢?或者有没有办法在sample.html中通过某种指示以便获取不同的参数?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/709
 
2769 次点击  
文章 [ 1 ]  |  最新文章 10 年前
Py站长
Reply   •   1 楼
Py站长    10 年前

简单的做法是:

{% if foo %}
  {% with a as b %}
    {% include "snipet.html" %}
  {% endwith %} 
{% else %}
  {% with bar as b %}
    {% include "snipet.html" %}
  {% endwith %} 
{% endif %}

http://stackoverflow.com/questions/7079804/django-with-tags-within-if-else-tags