I inserted new model fields into my existing model in Django yesterday. This is how it looks:
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:
When I look into the POST itself I see that none of my new fields were added!
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...
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.