Update data
-
I have a HTML code that's in charge of updating staff data. Box 1 (inspect the photo) presented by the text field updates the data. But box two and three, which presents data in the form of a deleting list (data from the model) does not update the changed data.
For convenience, code:
<form method="POST"> {% csrf_token %} <p> <label>Введите имя</label><br> <input type="text" name="name" value="{{person.name}}" /> </p>
<p> <label>Введите отдел</label><br> <select> {% for key in dep %} <option value="{{ key }}">{{ key }}</option> {% endfor %} </select> </p> <p> <label>Введите должность</label><br> <select> {% for key in pos %} <option value="{{ key }}">{{ key }}</option> {% endfor %} </select> </p> <input type="submit" value="Сохранить" >
</form>
And a picture to show.
views.py
def edit(request, id):
try:
person = Employee.objects.get(id=id)
dep = Department.objects.all()
pos = Position.objects.all()if request.method == "POST": person.name = request.POST.get("name") person.save() return redirect("base") else: return render(request, "manager_employees/edit.html", {"person": person, 'dep': dep, 'pos': pos})
except Employee.DoesNotExist:
return HttpResponseNotFound("<h2>Person not found</h2>")
What do I have to do with the data?
-
Look. You're getting from the input.
name
♪ You need to get in.html
<form method="POST"> {% csrf_token %} <p> <label>Введите имя</label><br> <input type="text" name="name" value="{{person.name}}" /> </p>
<p> <label>Введите отдел</label><br> <select name='select1'> {% for key in dep %} <option value="{{ key }}">{{ key }}</option> {% endfor %} </select> </p> <p> <label>Введите должность</label><br> <select name='select2'> {% for key in pos %} <option value="{{ key }}">{{ key }}</option> {% endfor %} </select> </p> <input type="submit" value="Сохранить" >
</form>
to
select
name
♪
And then get them as an input.person.select1 = request.POST.get("select1")
person.select2 = request.POST.get("select2")
and keep them in the model. Just like you did.
person.name = request.POST.get("name")