社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

初学者问个有关include的问题

jerry • 10 年前 • 2747 次点击  

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
 
2747 次点击  
文章 [ 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