Created
June 5, 2021 23:45
-
-
Save djotaku/defce46630cb69071aa577a170cba448 to your computer and use it in GitHub Desktop.
trying to get multiple choice results back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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