简体   繁体   中英

same row multiple times in a form in django

have a form by which user can enter details about some expenses but i want to have same row in the form again and again but couldn't find out how to do that :

在此处输入图片说明

if you see figure above this forms works well for 1 row of data , saves well but with more then 1 row it cant . Can someone suggest any way to do that . Below are the codes :

models.py

from django.db import models


class Expenditure(models.Model):
    exp_date = models.DateField("Expenditure_Date")
    description = models.CharField(max_length=500)
    amount = models.FloatField(default=0)
    currency = models.CharField(max_length=15,default="USD")

    class Meta:
        unique_together = ('exp_date', 'description',)

    def __unicode__(self):
        return self.description

forms.py

from django import forms
from moni.models import Expenditure
from django.contrib.admin.widgets import AdminDateWidget


class ExpenditureForm(forms.ModelForm):
    #exp_date = forms.DateField(help_text="Date")
    exp_date = forms.DateField(widget=AdminDateWidget)
    description = forms.CharField(max_length=500)
    amount = forms.FloatField(initial=0)
    currency = forms.CharField(widget=forms.HiddenInput(), initial="USD")

    # An inline class to provide additional information on the form.
    class Meta:
        # Provide an association between the ModelForm and a model
        model = Expenditure
        fields = ('exp_date', 'amount', 'description')

views.py

from django.template import RequestContext
from django.shortcuts import render_to_response
from moni.models import Expenditure
from moni.forms import ExpenditureForm

def add_expenditure(request):
    context = RequestContext(request)

    if request.method == 'POST':
        form = ExpenditureForm(request.POST)

        if form.is_valid():

            form.save(commit=True)
            return index(request)
        else:

            print form.errors
    else:

        form = ExpenditureForm()


    return render_to_response('moni/add_expenditure.html', {'form': form}, context)

add_expenditure.html

{% extends 'moni/base.html' %}

{% block title %}Add Shipment {% endblock %}

{% block body_block %}
        <h1>Add a Expenditure</h1>
        <p id="p_hide"> I am a paragraph to be hidden</p>
        <button id ="btn1">Hide Paragraph</button>

        <form id="expenditure_form" method="post" class="vDateField" action="/moni/add_expenditure/">

            {% csrf_token %}

            <table border=1>
                <tr><th><label >Date:</label></th> <th><label for="id_description">Description:</label></th><th><label for="id_amount">Amount:</label></th></tr>
            <tr><td><input class="vDateField"  name="exp_date" size="10" type="text" /></td><td>{{form.description}}</td><td>{{form.amount}}<input id="id_currency" name="currency" type="hidden" value="MYR" /></td></tr>
            <tr><td><input class="vDateField"  name="exp_date" size="10" type="text" /></td><td>{{form.description}}</td><td>{{form.amount}}<input id="id_currency" name="currency" type="hidden" value="MYR" /></td></tr>
        </table>
        <input type="submit" name="submit" value="Create Expenditure" />
    </form>
{% endblock %}

You should use ModelFormSets instead of ModelForm. And if you're going to add forms dynamically, use corresponding JavaScript plugin (since management form should be changed every time new form is added).

For that use Formeset function, Here is the idea for print form in multiple times

 ExpenditureFormSet = formset_factory(ExpenditureForm, extra=3,)

And views like

if formset.is_valid():
            for data in formset.cleaned_data:

And pass it into {formset} So html will print the extra 3 forms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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