same row multiple times in a form in django

倾然丶 夕夏残阳落幕 提交于 2019-12-24 07:19:22

问题


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 %}

回答1:


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




回答2:


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).



来源:https://stackoverflow.com/questions/24906234/same-row-multiple-times-in-a-form-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!