问题
In my template I have used for loop for some fields
<div class="register_div">
<p>Title:</p>
<p>{{ form.title }}</p>
</div>
<div class="register_div">
<p>Upload Images :</p>
<p>{{ form.image }}</p>
</div>
{% for item in product %}
<div class="register_div">
<p>{{ item.Name }} <input type="text" name="custom[{{item.id}}]"/></p>
</div>
{% endfor %}
<div class="register_div">
<p>Price:</p>
<p>{{ form.price }}</p>
</div>
As code shows there is one field which using for loops if that field has three records then in the form it shows three text boxes so that user can feed data to all three fields, but my table in database have only one col for his id and one for his vaue.
so how i can feed data to my database so that if user feels all three text boxes then it can be easily stored in to my database. If it is possible by three duplicate entry for title , image and price then its ok,But it should have take three different item name and there corresponding value.
e.g.
if there are three item names comes out from for loop then what user see is.
title
image
item 1 ---> its vaue
item 2 ---> its vaue
item 3 ---> its vaue
price
now it should we store in database like
id title image item_name item_value price
1 asd a.jpg item1 value 1 1111
2 asd a.jpg item2 value 2 555
3 asd a.jpg item3 value 3 789
or there is any another efficient way to do this please let me know
回答1:
The "efficient way" to do this is to make sure your models match this data, and then use Model FormSets.
If, for some arcane reason, you don't want to define models to match your data, you can use normal formsets instead.
If you aren't familiar with Django forms, I highly recommend you try to understand those first. This answer by yours truly has a fairly comprehensive walkthrough on django forms.
来源:https://stackoverflow.com/questions/12832693/issues-with-feeding-data-into-database-when-using-for-loop