def signup_view(request): if request.user.is_authenticated: return already_authenticated(request) if request.method == 'POST': form = SignupForm(request.POST) if not form.is_valid(): return render(request, 'accounts/signup.html', {'form': form}) username = form.cleaned_data['username'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] confirm_password = form.cleaned_data['confirm_password'] if password != confirm_password: form.add_error('confirm_password', "Passwords do not match!") return render(request, 'accounts/signup.html', {'form': form}) if User.objects.filter(username=username).exists(): form.add_error('username', "User with this username already exists.") return render(request, 'accounts/signup.html', {'form': form}) user = User.objects.create_user(username=username, email=email, password=password) user.save() profile = Profile.objects.create(user=user, avatar="images/user-fallback.png") profile.save() statistics = ProfileStatistics.objects.create(user=user) statistics.save() login(request, user) return redirect('accounts-profile', username=request.user.username) else: form = SignupForm() return render(request, 'accounts/signup.html', {'form': form})