list_users 変数にはユーザー名が含まれ、{{ i.username }} は使用可能なすべてのユーザーを一覧表示します。利用可能なすべてのユーザーをリストしています。ここで、ユーザーが選択したユーザー名のユーザー ID を渡し、ID を django ビューに取得したいと考えています。
<select name="select">
{% for i in list_users %}
<option value="{{ i.id }}" name="username">{{ i.username }}</option>
{% endfor %}
</select>
if request.POST.get('share'):
choices = request.POST.getlist('choice')
person = request.POST.getlist('username')
for i, j in zip(choices, person):
start_date = datetime.datetime.utcnow().replace(tzinfo=utc)
a = Share(users_id=log_id, files_id=i, shared_user_id=j, shared_date=start_date)
a.save()
return HttpResponseRedirect('/uploaded_files/')
共有をクリックすると、共有モデルに何も書き込まれません。しかし、私がこれを好きなら:
for i in choices:
start_date = datetime.datetime.utcnow().replace(tzinfo=utc)
a = Share(users_id=log_id, files_id=i, shared_user_id=2, shared_date=start_date)
a.save()
Shareモデルで書かれています。上記のコードで何が間違っていますか?