在模板中解析字典时出现问题

时间:2018-08-04 23:00:36

标签: django parsing templates

views.py-

def SubCategoryList(request):
    subcategory_form = SubcategoryForm(request.POST or None)
    context ={
    'form' : subcategory_form
    }
    #print(SubcategoryForm.POST.Category_id)
    if subcategory_form.is_valid():
        #print("Hi")
        print(subcategory_form.cleaned_data['Category_id'])
        qs = SubCategory.objects.filter(cat_id=subcategory_form.cleaned_data['Category_id'])
        data = {}
        for qss in qs:
            data[qss.subcategory_id] = qss.subcategory_name
        for k,v in data.items():
            print(data[k])
        print(data)
        return render(request,"Subcategory/cat_sub_list.html",data)

    return render(request,"Subcategory/sub_list.html",context)

cat_sub_list.html-

{% for key,v in data.items %} 
{{ v }}
{% endfor %}

sub_list.html

<form method='POST'> {% csrf_token%}
{{form}}
<button type="submit" class="btn btn-default">Submit</button>
</form>

{{form}}有一个整数输入字段。

输入值sub_list.html后,它将重定向到cat_sub_list.html。但解析后不打印数据(字典)的值。

0 个答案:

没有答案