Home > OS >  POST and models not matching - causing MultiValueDictKeyError
POST and models not matching - causing MultiValueDictKeyError

Time:08-09

I inserted new model fields into my existing model in Django yesterday. This is how it looks:

enter image description here

Then I ran these two commands-

python manage.py makemigrations
python manage.py migrate

In my views.py I try to get from the post request all the added fields, but it gives me this error:

enter image description here

When I look into the POST itself I see that none of my new fields were added!

enter image description here

I understand that the fields are non-exsistent in the new request.POST, so the error pops in this part of the code:

def updaterecord(request, id):
      group_name = request.POST['group']
      sale_rent = request.POST['sale_rent']
      street = request.POST['street']
      city = request.POST['city']
      rooms = request.POST['rooms']
      size = request.POST['size']
      floor = request.POST['floor']
      porch = request.POST['porch']
      storage = request.POST['storage']
      mamad = request.POST['mamad']
      elevator = request.POST['elevator']
      parking = request.POST['parking']
      price= request.POST['price']
      phone= request.POST['phone']
      date = request.POST['user_date']

I just don't understand how to solve this, is there any updates or commands I need to make? I don't remember how I solved it last time or how to avoid it in the future. Help please :-)

Additional Information- the sqliteDB does have all the fields in it, so this is how my main website looks...

enter image description here

CodePudding user response:

I found the error! Make sure in the HTML you are using the value under the input "name" matches the field your trying to get in the POST request!!!

CodePudding user response:

Html tag attributes / View with strings hardcoded / your model attributes should always be intact, and this could be considered as complexity from a maintenance perspective.

Take a look at django model forms, or at simply at django forms.

  • Related