Django 错误的 request.POST 属性值已保存

Posted

技术标签:

【中文标题】Django 错误的 request.POST 属性值已保存【英文标题】:Django wrong request.POST attribute value saved 【发布时间】:2018-07-10 07:26:11 【问题描述】:

我遇到了代码将 request.POST['name'] 值保存到数据库中的假设.Name 字段的问题,但在这种情况下只有列表中的最后一个值是“B”。我怎样才能让它根据各自的表单将前缀“A”和“B”保存到数据库字段名称中。当我调试时,我看到'A'和'B'都带有键'name'。

views.py

from django.shortcuts import render
from .forms import modelformset_factory, AssumptionsForm
from .models import Assumptions
from django.core.exceptions import ValidationError


model_names = ['A', 'B']

def get_assumptions(request):

    AssumptionFormset = modelformset_factory(
    Assumptions, form=AssumptionsForm, extra=5)

    if request.method == 'POST':



        formsets = [AssumptionFormset(request.POST, prefix=thing) for thing in model_names]

        if all([formset.is_valid() for formset in formsets]):

            for formset in formsets:

                for form in formset:

                    assumption_data = form.save(commit=False)
                    assumption_data.Name = request.POST.get('name')
                    assumption_data.save()   



    else:

        formsets = [AssumptionFormset(prefix=thing) for thing in model_names]

    return render(request, 'assumptions.html', 'formsets': formsets)

assumptions.html

<div class="form">
<form action="" method="post">
% for formset in formsets % 
    % csrf_token %
     formset.management_form 
     formset.non_form_errors.as_ul  
    <h1>formset.prefix</h1>
    <table id="formset" class="form">
    % for form in formset.forms %
    % if forloop.first %
    <thead><tr>
        % for field in form.visible_fields %
        <th> field.label|capfirst </th>
        % endfor %
    </tr></thead>
    % endif %
    <tr class="% cycle 'row1' 'row2' %">
        % for field in form.visible_fields %
            <td>
            # Include the hidden fields in the form #
            % if forloop.first %
            % for hidden in form.hidden_fields %
             hidden 
            % endfor %
            % endif %
             field.errors.as_ul 
             field 
            </td>
        % endfor %
    </tr>
    % endfor %
    </table>
    <input type="hidden" id=formset.prefix name='name', value=formset.prefix />
% endfor %    
<input type="submit" value="Submit">
</form>
</div>

models.py

from django.db import models
from django.forms import ModelForm

class Assumptions(models.Model):

    Worst = models.FloatField(null=True, blank=True, default=None)
    Base = models.FloatField(null=True, blank=True, default=None)
    Best = models.FloatField(null=True, blank=True, default=None)
    Name = models.TextField(null=True, blank=True, default=None)

forms.py

from django import forms
from django.forms import modelformset_factory, ModelForm
from .models import Assumptions

class AssumptionsForm(ModelForm):

    class Meta:
        model = Assumptions
        fields = '__all__'
        exclude = ['Name']

【问题讨论】:

【参考方案1】:

我想通了。我应该使用 getlist 方法,然后遍历值。

values = request.POST.getlist('name')

干杯。

【讨论】:

以上是关于Django 错误的 request.POST 属性值已保存的主要内容,如果未能解决你的问题,请参考以下文章

Django1.11自带authenticate认证错误问题

如何让 Django 视图返回表单错误

Django组件之form

django 获取 POST 请求值的几种方法

Django之form表单

Django Rest 框架:request.Post 与 request.data?