Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using for loop in my template and for different id of each record i am using arrays

{% for item in product %}

<div class="register_div">


    <p><label for="id[{{ item.id }}]">{{ item.Name }} </label> <input type="text" name="custom[{{item.id}}]"/></p>

</div>

{% endfor %}

Now when in my views i want to save this data to my database.But firstly i checked that whether my array return something or not.So i just try print that as.

q = upload_form.data['custom[]']

or

 q = upload_form.data['custom']

but it gives me this error

"Key 'custom[]' **OR** key custom not found in <QueryDict: {u'custom[2]': [u's'], u'custom[1]': [u'a'], u'price': [u''], u'title': [u''], u'customatt': [u'', u''], u'csrfmiddlewaretoken': [u'up4Ipd5L13Efk14MI3ia2FRYScMwMJLz']}>"

but if i print this

q = upload_form.data['custom[1]']

then it display the value of array 1.

so please suggest me a better way to do this how i can display all values inside my array in views.py

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
201 views
Welcome To Ask or Share your Answers For Others

1 Answer

As upload_form.data is a dictionary, the key 'custom[]' simply doesn't exist. Try something like:

custom_values = {}    
for key, value in in upload_form.data.items():
    if key.startswith('custom'):
        custom_values[key]=value

The dictionary custom_values holds now all your 'custom' form values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...