无法调用并验证Django中的formset

时间:2017-07-24 22:05:30

标签: django django-views

我有一个来自我在django视图中工作的内容,我似乎无法弄清楚如何调用formset来验证它并从表单中获取值。有没有办法让我在用户提交后调用formset然后在我开始将用户输入收集到表单之前验证所有字段...这是我的代码:还有一种方法可以创建formset中每个表单的前缀都是唯一的,这样当我抓取值时,我可以比默认id或字段标签更容易。

验证码将在第26行和第26行中。 27。

def addTransaction(request, groupId, recordId):
    if 'username' not in request.session:
        return redirect('login')
    else:
        username = request.session['username']
        currentUser = User.objects.get(username = username)
        group = Group.objects.get(id=groupId)
        record = Record.objects.get(id=recordId)
        transactions = Transaction.objects.filter(record=recordId).all().count()
        SplitFormSet = formset_factory(IndividualSplitTransactionForm, extra=transactions)
        if request.method == 'POST':
            if record.split == 1:
                form = EvenSplitTransactionForm(request.POST)
                if form.is_valid():
                    cd = form.cleaned_data
                    amount = cd['amount']
                    description = cd['description']
                    split_amount = SplitEven(record, amount)
                    for trans in transactions:
                        if trans.record.id == record.id:
                            trans.description = description
                            trans.amount = split_amount
                            trans.save()
                    return redirect('group_home', groupId=group.id)
            if record.split == 2:
                formset = SplitFormSet(request.POST)
                if formset.is_valid():
                    cheese = 'cheese'
                    print(cheese)
                # this is where i want to iterate though the formset and get the info
                # that i need from the forms that the user submitted.
                # form = formList(request.POST)
                # if form.is_valid():
                #     cd = form.cleaned_data
                #     for transaction in transactions:
                #         amount = transaction.user.username + '-amount'
                #         description = transaction.user.username + '-description'
                #         currentAmount = cd[amount]
                #         currentDescription = cd[description]
                #         transaction.amount = currentAmount
                #         transaction.description = currentDescription
                #         transaction.save()
                #     return redirect('group_home', groupId=group.id)
            return redirect('accounts')
        else:
            if record.split == 1:
                form = EvenSplitTransactionForm()
                message = 'fill out the form below'
                parameters = {
                    'record':record,
                    'form':form,
                    'message':message,
                    'transactions':transactions,
                }
                return render(request, 'tabs/add_even_transactions.html', parameters)
            if record.split == 2:
                print(SplitFormSet)
                message = 'message'
                parameters = {
                    'record':record,
                    'SplitFormSet':SplitFormSet,
                    'message':message,
                    'transactions':transactions,
                }
                return render(request, 'tabs/add_individual_transaction.html', parameters)

已更新

我更新了一些代码以包含建议的修复:

            if record.split == 2:
                formset = SplitFormSet(request.POST)
                for form in formset:
                    if form.is_valid():
                        cheese = 'cheese'
                        print(cheese)

我收到以下错误...

ValidationError at /38/69/add_transaction/
['ManagementForm data is missing or has been tampered with']
------
for form in formset: 

HTML文件:

{% extends "base.html" %}

{% block content %}
  <h1>Add members to {{record.name}}</h1>
  {% if message %}
    <p>{{message}}</p>
  {% endif %}
  <form action="." method="POST">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in SplitFormSet %}
      {{ form.as_p }}
    {% endfor %}
    <p>Tax: <input type="text" name="tax" value=""></p>
    <p>Tip: <input type="text" name="tip" value=""></p>
    <input type="submit" name="submit" value="submit">
  </form>
{% endblock %}

0 个答案:

没有答案