![](/img/trans.png)
[英]Django cannot access cannot access a dicitionary passed in context using a variable created in the template
[英]Django cannot access context variable in Jinja template
我无法访问 Jinja HTML 模板中的上下文变量。 以下是代码。
视图.py
class Home(views.APIView):
renderer_classes = [TemplateHTMLRenderer]
def get(self,request):
context = home(request)
return Response(context)
def get_template_names(self):
#code to return the correct HTML template
return [template_name]
“get”中的调用“home(request)”返回一个用户对象和字典。 以下是代码
def home(request):
#lines of code
context={"form":form,'questions':get_questions(),'user_ques':user_questions,'dates':dates}
return context
现在,在模板中我可以访问“问题”和日期变量。 我无法访问“表单”的初始值。 “form”是“forms.Form”类型。 当我调试时,我可以看到这些值填充在表单变量中,但是当我在模板端检查时,它们是“无”。 与“user_ques”相同的是值的字典,但在模板端它的“{}”。
访问模板中的表单变量,如
<label for="height" class="col-sm-2 control-label">{{form.height_feet.initial}}</label>
这里 height_feet 的定义形式为
forms.IntegerField(min_value=4,max_value=8, required=True)
我不确定发生了什么。 任何方向都会有所帮助!
看问题后。 我通过“ajax”调用将调用从浏览器路由到 Django 后端。 响应返回了 ajax 调用,我忽略了响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.