Exemple #1
0
    def post(self, request):

        form = SignUpForm(request.POST)

        if form.is_valid():
            cd = form.cleaned_data
            first_name = cd.get("first_name")
            last_name = cd.get("last_name")
            username = cd.get("username")
            email = cd.get("email")
            password1 = cd.get("password1")
            password2 = cd.get("password2")
            blogtitle = cd.get("blogtitle")

            possible_blogs = Blog.objects.filter(blog_title=blogtitle)
            possible_email = User.objects.filter(email=email)

            if len(possible_email) > 0:
                form.add_error(None, 'This email is already in use')

            if len(possible_blogs) > 0:
                form.add_error(None, 'This blog title is already in use')

            if len(possible_blogs) == 0 and len(possible_email) == 0:
                new_user = User.objects.create_user(username, email, password1, first_name=first_name, last_name=last_name)
                new_blog = Blog.objects.create(blog_title=blogtitle, user=new_user)
                new_user.save()
                new_blog.save()
                form = SignUpForm()
                message = "¡Usuario creado con éxito!"
                messages.success(request, message)
                return redirect('login_page')

        context = {'form': form}
        return render(request, "signup_form.html", context)
Exemple #2
0
 def post(self, request):
     form = SignUpForm(request.POST)
     if form.is_valid():
         form.save()
         username = form.cleaned_data.get('username')
         raw_password = form.cleaned_data.get('password1')
         user = authenticate(username=username, password=raw_password)
         django_login(request, user)
         return redirect('home_page')
     else:
         form.add_error(None, "Datos no válidos")
     return render(request, 'signup_form.html', {'form': form})