我跟着this link并在我的django应用程序中尝试了类似的东西。在我的views.py中我定义了这样的东西:
def acadprogram(request):
name = Program_requirement_category.objects.get(name='AS.SCIENCE')
student_id = 2773951
values = {'student_id':student_id}
descendants = name.get_descendants(include_self=True)
return render_to_response("courses.html", {'nodes':descendants.all()}, values, context_instance = RequestContext(request))
我尝试像这样访问student_id:
<h3><font color=#428BCA>Caleb Streeter {{student_id}}</font></h3>
但是django给我一个错误如下:
render_to_string() got multiple values for keyword argument 'context_instance'
那么我如何尝试通过 render_to_response传递值?
答案 0 :(得分:2)
两个选项:
将值编辑为
values = {'student_id':student_id, 'nodes':descendants.all() }
并返回
render_to_response("courses.html", values, context_instance = RequestContext(request))
或只是
return render_to_response("courses.html", {'nodes':descendants.all(), 'student_id':student_id}, context_instance = RequestContext(request))
答案 1 :(得分:2)
您的student_id变量需要添加到您拥有节点的字典中。这样的事情应该有效:
def acadprogram(request):
name = Program_requirement_category.objects.get(name='AS.SCIENCE')
student_id = 2773951
descendants = name.get_descendants(include_self=True)
values = {'student_id':student_id, 'nodes':descendants.all()}
return render_to_response("courses.html", values, context_instance = RequestContext(request))
答案 2 :(得分:1)
试试这个:
from django.shortcuts import render
def acadprogram(request):
values = {}
name = Program_requirement_category.objects.get(name='AS.SCIENCE')
values['student_id'] = student_id = 2773951
values['descendants'] = name.get_descendants(include_self=True)
return render(request, "courses.html", values)
答案 3 :(得分:0)
你可以简单地使用locals()将局部变量传递给模板渲染。
return render_to_response('your/template.html',
locals(),
context_instance=RequestContext(request)