2

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

4
  • Its not very clear how you are using forms? Are those items part of a modelform and formset? If not, how does the user submit and update to them, is there a button to be clicked for each item? If yes then the action to update has to be taken on button click whether per-item or in bulk is as per your form design. Commented Oct 11, 2012 at 5:49
  • 1
    At first glance, this appears to be a good use case for a ModelForm ( and a FormSet ). Commented Oct 11, 2012 at 6:20
  • What is the custom variable (in your template)? Commented Oct 11, 2012 at 6:26
  • I have just made the array please look at this question may be this explain something right about my problem stackoverflow.com/questions/12833567/… Commented Oct 11, 2012 at 6:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.