如何在Django中传递上下文值以包含模板

时间:2016-03-24 09:05:09

标签: python django django-templates django-views

我想将值动态传递给Django中的模板,我有以下代码。 帮我提一些建议。

view.py

account_detail(request):
 url = request.get_full_path()
 temp = url.split('/')
 id1 = int(temp[2])
 print url
 info = Patient_details.objects.get(id=id1)
 context = Context({'info': info,'id':id1})
 return render(request, 'patient/dashboard_d.html', context)

模板1:

{% include "sidebar.html" with url={{ id }}%}

模板2: <li><a href="../../{{ url }}/dashboard">Dashboard</a></li>

1 个答案:

答案 0 :(得分:2)

作为猜测,我猜测您的问题与包含有关,您不需要包含{{ }}代码。

{% include "sidebar.html" with url=id %}

虽然当您使用include时,您传递给该模板的模板的上下文也会被传递,因此您可以这样做

<li><a href="../../{{ id }}/dashboard">Dashboard</a></li>

虽然这仍然是错误的,因为你应该使用{% url %} template tag