验证错误:您的ManagementForm数据丢失或已被篡改过'

时间:2015-11-06 22:07:30

标签: django validation formset

我正在尝试更新多个对象值,但是当我点击更新'我收到以下错误:

ValidationError at /projects/updatevalues/
[u'ManagementForm data is missing or has been tampered with']

它指出我的功能中的行如下:

if formset.is_valid():

我正在努力去理解我做错了什么。有什么帮助吗?

功能

def manage_zonesubs_values(request):
    context = RequestContext(request)
    value1=str(request.POST.get('dropdown1'))
    value2=str(request.POST.get('dropdown2'))
    value3=str(request.POST.get('dropdown3'))
    formset = modelformset_factory(ZoneSubStage, fields=('zone','substage','value'),max_num=2)
    zonesubslist = ZoneSubStage.objects.filter(substage__stage__project__name=value1,
                                               substage__stage__stage_name=value2,
                                               zone__zone_name=value3)
    if request.method == 'POST':
        print request.POST

        try:
            formset = formset(request.POST, queryset=zonesubslist)

        except ValidationError:
            formset = None
            print 'problem'

        if formset.is_valid():
            formset.save()
    else:
        formset = formset(queryset=zonesubslist)

    return render_to_response('autostages/update_values.html', {'formset': formset}, context)

局部变量:

value1  
'project1'
zonesubslist    
[<ZoneSubStage: z1 S1.1>, <ZoneSubStage: z1 S1.2>, <ZoneSubStage: z1 S1.3>]
request 
'<WSGIRequest\npath:/projects/updatevalues/,\nGET:<QueryDict: {}>,\nPOST:<QueryDict: {u\'csrfmiddlewaretoken\': [u\'pFs9fj5MLQML7KLYHXFeCWTSvuMFEoML\'], u\'dropdown1\': [u\'project1\'], u\'dropdown2\': [u\'S1\'], u\'dropdown3\': [u\'z1\']}>,\nCOOKIES:{\'csrftoken\': \'pFs9fj5MLQML7KLYHXFeCWTSvuMFEoML\',\n \'sessionid\': \'oi3spojg2j2yqby5u3tw4i5xf1tt96ik\'},\nMETA:{\'COLORFGBG\': \'15;0\',\n \'COMP_WORDBREAKS\': \' \\t\\n"\\\'><;|&(:\',\n \'CONTENT_LENGTH\': \'97\',\n \'CONTENT_TYPE\': \'application/x-www-form-urlencoded\',\n u\'CSRF_COOKIE\': u\'pFs9fj5MLQML7KLYHXFeCWTSvuMFEoML\',\n \'DBUS_SESSION_BUS_ADDRESS\': \'unix:abstract=/tmp/dbus-cDemiVNVSB,guid=dac2f692b096bd7aa541b450563cef43\',\n \'DEFAULTS_PATH\': \'/usr/share/gconf/default.default.path\',\n \'DESKTOP_SESSION\': \'default\',\n \'DISPLAY\': \':0\',\n \'DJANGO_SETTINGS_MODULE\': \'projectmanage.settings\',\n \'GATEWAY_INTERFACE\': \'CGI/1.1\',\n \'GDMSESSION\': \'default\',\n \'GDM_XSERVER_LOCATION\': \'local\',\n \'GS_LIB\': \'/home/dkr103/.fonts\',\n \'GTK2_RC_FILES\': \'/etc/gtk-2.0/gtkrc:/home/dkr103/.gtkrc-2.0:/home/dkr103/.kde/share/config/gtkrc-2.0\',\n \'GTK_RC_FILES\': \'/etc/gtk/gtkrc:/home/dkr103/.gtkrc:/home/dkr103/.kde/share/config/gtkrc\',\n \'HOME\': \'/home/dkr103\',\n \'HTTP_ACCEPT\': \'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\',\n \'HTTP_ACCEPT_ENCODING\': \'gzip, deflate\',\n \'HTTP_ACCEPT_LANGUAGE\': \'en-US,en;q=0.8\',\n \'HTTP_CACHE_CONTROL\': \'max-age=0\',\n \'HTTP_CONNECTION\': \'keep-alive\',\n \'HTTP_COOKIE\': \'sessionid=oi3spojg2j2yqby5u3tw4i5xf1tt96ik; csrftoken=pFs9fj5MLQML7KLYHXFeCWTSvuMFEoML\',\n \'HTTP_HOST\': \'localhost:8000\',\n \'HTTP_ORIGIN\': \'http://localhost:8000\',\n \'HTTP_REFERER\': \'http://localhost:8000/projects/choosevalues/\',\n \'HTTP_USER_AGENT\': \'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/43.0.2357.81 Chrome/43.0.2357.81 Safari/537.36\',\n \'KDE_FULL_SESSION\': \'true\',\n \'KDE_MULTIHEAD\': \'false\',\n \'KDE_SESSION_UID\': \'1000\',\n \'KDE_SESSION_VERSION\': \'4\',\n \'KONSOLE_DBUS_SERVICE\': \':1.58\',\n \'KONSOLE_DBUS_SESSION\': \'/Sessions/1\',\n \'KONSOLE_PROFILE_NAME\': \'LinuxMint\',\n \'LANG\': \'en_GB.UTF-8\',\n \'LANGUAGE\': \'\',\n \'LESSCLOSE\': \'/usr/bin/lesspipe %s %s\',\n \'LESSOPEN\': \'| /usr/bin/lesspipe %s\',\n \'LOGNAME\': \'dkr103\',\n \'LS_COLORS\': \'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:*.JPG=01;35:*.GIF=01;35:*.jpg=01;35:*.gif=01;35:*.jpeg=01;35:*.pcx=01;35:*.png=01;35:*.pnm=01;35:*.bz2=01;31:*.mpg=01;38:*.mpeg=01;38:*.MPG=01;38:*.MPEG=01;38:*.m4v=01;038:*.mp4=01;038:*.swf=01;038:*.avi=01;38:*.AVI=01;38:*.wmv=01;38:*.WMV=01;38:*.asf=01;38:*.ASF=01;38:*.mov=01;38:*.MOV=01;38:*.mp3=01;39:*.ogg=01;39:*.MP3=01;39:*.Mp3=01;39\',\n \'MANDATORY_PATH\': \'/usr/share/gco... <trimmed 6781 bytes string>
value3  
'z1'
value2  
'S1'
context 
[{'False': False, 'None': None, 'True': True}, {}, {}]
formset 
<django.forms.formsets.ZoneSubStageFormFormSet object at 0x7f641c0d1290>

update_values.html

<form action="" method="post">{% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
            {{ form.as_p }}
        {% endfor %}
    <input type="submit" value="Update" />
</form>

1 个答案:

答案 0 :(得分:0)

我认为您在模板中缺少管理表单。 Formsets需要隐藏的管理表单,告诉django集合中有多少表单。