Skip to content

Instantly share code, notes, and snippets.

@djotaku
Created June 5, 2021 23:45
Show Gist options
  • Save djotaku/defce46630cb69071aa577a170cba448 to your computer and use it in GitHub Desktop.
Save djotaku/defce46630cb69071aa577a170cba448 to your computer and use it in GitHub Desktop.
trying to get multiple choice results back
in forms.py:
def create_user_choices():
users = User.objects.get_queryset()
return [(user, f"{user.first_name} {user.last_name}") for user in users]
class RandomizeForm(forms.Form):
def __init__(self, *args, **kwargs):
super(RandomizeForm, self).__init__(*args, **kwargs)
self.fields['participants'] = forms.MultipleChoiceField(choices=create_user_choices(),
widget=forms.CheckboxSelectMultiple)
class Meta:
fields = ('participants', )
in views.py:
@login_required
@user_passes_test(lambda u: u.is_superuser)
def randomizer(request):
if request.method == "POST":
user_selection_form = RandomizeForm(data=request.POST)
users = user_selection_form['participants']
print("****************")
print(users)
print("*****************")
else:
user_selection_form = RandomizeForm()
return render(request, 'practicum/randomize.html', {'user_selection_form': user_selection_form})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment