New to Django here. I am setting up a registration page and I would like to ensure that someone who is currently logged in can't create a new account.
So far I have a form like this:
class RegisterView(FormView):
template_name = 'users/register_user.html'
success_url = 'thank_you'
def get(self, request):
if request.user.is_authenticated():
# If a user is logged in, redirect them to a page informing them of such
return render(request, 'users/already_logged_in.html')
else:
# would like to direct the user to the normal registration page
def post(self, request):
user_form = UserCreateForm(request.POST)
if user_form.is_valid():
username = user_form.clean_username()
password = user_form.clean_password2()
user_form.save()
user = authenticate(username=username,
password=password)
login(request, user)
return render(request, 'home.html')
return render(request, 'register_user.html', {'form': user_form})
I suspect that I might have to use mixins (I'm also confused about what they are) so if they are part of the solution please expand a little bit.
What should I include after the "else" to get it to pass the usual registration page?
Thank you for your help in advance.
Just return what the parent FormView would have returned
def get(self, request):
if request.user.is_authenticated():
# If a user is logged in, redirect them to a page informing them of such
return render(request, 'users/already_logged_in.html')
else:
return super(RegisterView, self).get(request)
You'll need to set form_class on your view, rather than in your post Method, look at the django docs for how to use FormView https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-editing/#formview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With