Last active
April 13, 2020 09:58
-
-
Save jqn/90545f532cdbc27e375269c7084078f6 to your computer and use it in GitHub Desktop.
SuperTuber TutorUserSignUpForm
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
| class TutorUserSignUpForm(forms.ModelForm): | |
| def __init__(self, *args, **kwargs): | |
| self.user = kwargs.pop('user', None) | |
| super(TutorUserSignUpForm, self).__init__(*args, **kwargs) | |
| self.helper = FormHelper(self) | |
| profile = Profile.objects.get(pk=self.user.pk) | |
| self.initial['firstname'] = profile.firstname | |
| self.initial['lastname'] = profile.lastname | |
| class Meta: | |
| model = TUser # change model to TutorProfile and add bio | |
| fields = ['username', 'firstname', 'lastname', | |
| 'email', 'phone_number', 'subjects', 'image'] | |
| widgets = { | |
| 'username': forms.TextInput( | |
| attrs={ | |
| 'class': 'form-control', | |
| } | |
| ), | |
| 'firstname': forms.TextInput( | |
| attrs={ | |
| 'class': 'form-control', | |
| 'value': "Hello world" | |
| } | |
| ), | |
| 'lastname': forms.TextInput( | |
| attrs={ | |
| 'class': 'form-control' | |
| } | |
| ), | |
| 'email': forms.EmailInput( | |
| attrs={ | |
| 'class': 'form-control' | |
| } | |
| ), | |
| 'phone_number': forms.NumberInput( | |
| attrs={ | |
| 'class': 'form-control' | |
| } | |
| ), | |
| 'subjects': forms.Select( | |
| choices=SUBJECT_CHOICES, | |
| attrs={ | |
| 'class': 'form-control' | |
| } | |
| ), | |
| 'image': forms.ClearableFileInput( | |
| attrs={ | |
| 'class': 'form-control' | |
| } | |
| ), | |
| } |
Your views also need to be updated
class TutorRegister(CreateView):
model = TUser
form_class = TutorUserSignUpForm
template_name = 'FindTutors/tutor_signup.html' # correct form HTML
def form_valid(self, form):
user = form.save(commit=False)
user.is_tutor = True
user.save()
return redirect('/home/tutors/') # Go back to the table of tutors
def get_form_kwargs(self):
kwargs = super(TutorRegister, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
Basically I'm passing the logged-in user to the form in forms.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I added to the TutorSignUpForm
and this for the select subject field