简体   繁体   English

将字典传递给视图,具有多个键:值对

[英]passing dictionary to view ,with multiple key:value pairs

def status_(request,id):
    
    stages=['Vendor Quotation','Estimate Generation','Purchase Requsition','Purchase Order','Provision To Invoice','Reimbursement Invoice','Request Receipt#(PTI+RI+PO)','Receipt# Received(Updated PTI)','Request Sales Tax Invoice','Uploaded to Jazz Portal']    
    ctx={'invoice':Invoices.objects.get(pk=id),'stages':stages}
    return render(request,"Invoices/status.html",ctx)

Hi I am trying to pass multiple objects of data for displaying to the template for displaying, I am creating a ctx dictionary with multiple 2 key value pairs,second Key-value pair is array,when i access this array in by using您好我正在尝试将用于显示的多个数据对象传递给用于显示的模板,我正在创建一个具有多个 2 个键值对的ctx字典,第二个键值对是数组,当我使用访问此数组时

{% for idx in range(0,len(ctx.get("stages")) %}

I get following parsing error我收到以下解析错误

**Could not parse the remainder: '(0,len(ctx.get("stages"))' from 'range(0,len(ctx.get("stages"))'**

You can not make function calls in Django templates with parameters, hence the above wiull not work, but likely you do not need this anyway, you can simply enumerate with:您不能在 Django 模板中使用参数进行 function 调用,因此上述方法将不起作用,但您可能不需要这个,您可以简单地枚举:

{% for stage in stages %}
    {{ stage }}
{% endfor %}

If you really need to enumerate, then you can pass a range object through the context, but then probably a next problem pops up: you can not subscript with a variable either.如果你真的需要枚举,那么你可以通过上下文传递一个范围 object ,但是可能会弹出下一个问题:你也不能用变量下标。 It will require using a lot of extra template filters, making it very complicated.这将需要使用大量额外的模板过滤器,使其非常复杂。 Templates are normally not used to implement business logic , therefore Django has a template engine that does not support complicated function calls, etc. to give the developer an incentive to move the business logic to the view.模板通常不用于实现业务逻辑,因此 Django 有一个模板引擎,不支持复杂的 function 调用等,以激励开发人员将业务逻辑移至视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM