I'm working on a Django (4.0+) project,, where I'm using the default User
model for login, and a custom form for signup
, and I have a Boolean field named: blogger
in signup form, if user check this, i have to set is_staff
true, otherwise it goes as normal user.
Here's what I have:
Registration Form:
class UserRegistrationForm(UserCreationForm):
first_name = forms.CharField(max_length=101)
last_name = forms.CharField(max_length=101)
email = forms.EmailField()
blogger = forms.BooleanField()
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2', 'blogger', 'is_staff']
views.py:
def register(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
print(form['blogger'])
if form['blogger']:
form['is_staff'] = True
form.save()
messages.success(request, f'Your account has been created. You can log in now!')
return redirect('login')
else:
print(form.errors)
else:
form = UserRegistrationForm()
context = {'form': form}
return render(request, 'users/signup.html', context)
Its giving an error as:
TypeError: 'UserRegistrationForm' object does not support item assignment
How can i achieve that?