Made a user edit form, where, inter alia, the ID in the database is also transmitted. The list of initial parameters passed as a regular list info

Form Screenshot

Here is its HTML code:

 <form id="edituser" method="post" action="/user/save" enctype="multipart/form-data"> <label for="dbid">ID записи в БД: </label> <input form="edituser" type="text" name="dbid" id="dbid" value="{{ info[0] }}" disabled> <label for="name">ФИО сотрудника: </label> <input form="edituser" type="text" name="name" id="name" value="{{ info[3] }}" required> <label for="branch">Отдел: </label> <input form="edituser" type="text" name="branch" id="branch" value="{{ info[4] }}" required> <label for="uname">Логин: </label> <input form="edituser" type="text" name="uname" id="uname" value="{{ info[2] }}" required> <label for="ugroup">Категория доступа: </label> <input form="edituser" type="text" name="ugroup" id="ugroup" value="{{ info[1] }}" required> <label for="passwd">Пароль: </label> <input form="edituser" type="password" name="passwd" id="passwd"> <div class="buttons"> <input form="edituser" type="submit" class="button" name="save" id="save" value="Сохранить"> <a href="/manage" class="button" id="cancel">Отмена</a> </div> </form> 

And the request fragment itself:

 @get("/user/save") @post("/user/save") def save_user(): current_user = request.get_cookie("account", secret=SECRET) user = UserManager(current_user) if request.POST.get("save"): dbid = int(request.forms.get("dbid")) name = (request.forms.get("name")).decode("utf-8") branch = (request.forms.get("barnch")).decode("utf-8") uname = (request.forms.get("uname")).decode("utf-8") ugroup = (request.forms.get("ugroup")).decode("utf-8") passwd = (request.forms.get("passwd")).decode("utf-8") success_edit = user.edit_user(dbid, ugroup, uname, name, branch, passwd) if success_edit: redirect("/manage") else: return "error" 

When I click "Save", I get an error

 dbid = int(request.forms.get("dbid")), referer: TypeError: int() argument must be a string or a number, not 'NoneType' 

If you comment out the erroneous line, and pass a specific number (in this case, 3) to the user.edit_user function instead of dbid , everything works, all data from the form is read and written to the database.

What can be wrong?

dbid = (request.forms.get("dbid")).decode("utf-8") - that doesn't work either :)

    1 answer 1

    The point here is that disabled ( disabled ) fields do not send their contents . Those. The browser itself does not send (should not send) information, and not you somehow incorrectly accept it. Also read about what other fields do not send their data in this section , there are still surprises, such as the "disabled" checkbox also will not send anything. There are several alternatives:

    • use readonly attribute . You can click on this field with the mouse, but you cannot change it and the data is sent in the request (ie, may be successfull from the specification):

    <input form="edituser" type="text" name="dbid" id="dbid" value="{{ info[0] }}" readonly>

    • use hidden field doublers with the same name as the main field:

    <input form="edituser" type="text" name="dbid" id="dbid" value="{{ info[0] }}" readonly> <input form="edituser" type="hidden" name="dbid" id="dbid_real" value="{{info[0] }}">