各ユーザーに複数のユーザー名と電話番号が関連付けられたデータベースがあります。私が作成した Django テンプレートでは、ユーザーに名前のリストが表示され、ユーザーは名前または複数の名前をクリックでき、データベースは名前に割り当てられた電話番号で応答します。ただし、Django テンプレート内で for ループを使用して、データベース内の名前を繰り返し処理し、ユーザーに表示します。これは、カウントが変更される可能性があるためです。1 つの名前を選択すると正しく動作しますが、複数の名前を選択すると、すべての名前を表示するのではなく、選択した姓が取得されます。このエラーは、すべての入力に同じ「名前」が割り当てられている for ループ ソリューションによるものです。私がこれにどのようにアプローチできるかについて誰にも考えがありますか?
私のビューフォーム:
def select_contact(request):
alldata = identity_log.objects.values_list("first_name", flat=True)
#https://docs.djangoproject.com/en/4.0/ref/models/querysets/
checkform = contact_form(request.POST or None)
context = {'alldata': alldata}
print(checkform)
display_type = request.POST.get("contact_option", None)
if display_type in alldata:
print(display_type)
return render(request, 'message_me/select_contact.html', context)
私のテンプレート:
{% extends "base.html" %}
{% load static %}
{% block body %}
<p>Please select your favorite Web language:</p>
{% for x in alldata %}
<form id="contact_option" role="form" action="" method="POST">
{% csrf_token %}
<input type="checkbox" id="contact_option" name="contact_option" value="{{x}}">
<label for="contact_option">{{x}}</label><br>
{% endfor %}
<div class="row">
<div class="col-md-12"> <input type="submit" name="submit" class="btn btn-success btn-send pt-2 btn-block " value="Continue"> </div>
</div>
</form>
{% endblock %}