Skip to content

Instantly share code, notes, and snippets.

@jqn
Last active April 13, 2020 09:58
Show Gist options
  • Save jqn/90545f532cdbc27e375269c7084078f6 to your computer and use it in GitHub Desktop.
Save jqn/90545f532cdbc27e375269c7084078f6 to your computer and use it in GitHub Desktop.
SuperTuber TutorUserSignUpForm
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'
}
),
}
@jqn
Copy link
Author

jqn commented Apr 13, 2020

This is what I added to the TutorSignUpForm

    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.user_type

and this for the select subject field

SUBJECT_CHOICES = (
    ('Computer Science', 'Computer Science'),
    ('Biology', 'Biology'),
    ('Chemistry', 'Chemistry'),
    ('Physics', 'Physics'),
    ('Math', 'Math'),
    ('English', 'English'),
    ('Algebra', 'Algebra'),
    ('Calculus', 'Calculus'),
    ('Geometry', 'Geometry'),
    ('Language', 'Language'),
    ('Reading', 'Reading'),
    ('Music', 'Music'),
)
   'subjects': forms.Select(
                choices=SUBJECT_CHOICES,
                attrs={
                    'class': 'form-control'
                }
            ),

@jqn
Copy link
Author

jqn commented Apr 13, 2020

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