Exemplo n.º 1
0
    def post(self, request):
        form = RegisterForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            password2 = form.cleaned_data['password2']
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']

            if password != password2:
                return render(request, 'register.html', {
                    'form': form,
                    'message': 'The password does not match'
                })
            elif User.objects.filter(email=email):
                return render(request, 'register.html', {
                    'form': form,
                    'message': 'email already used by another user'
                })
            else:
                try:
                    User.objects.create_user(username=username,
                                             email=email,
                                             password=password,
                                             first_name=first_name,
                                             last_name=last_name)
                    return render(request, 'register.html', {
                        'form': form,
                        'success': 'User created'
                    })
                except:
                    return render(request, 'register.html', {
                        'form': form,
                        'message': 'Username already exist'
                    })
        else:
            form = RegisterForm()
            return render(request, 'register.html', {
                'form': form,
                'message': 'Invalid data'
            })
Exemplo n.º 2
0
def register_view(request):
    if request.method == "POST":
        context = login_page_data()
        form = RegisterForm(request.POST, prefix="register")
        if form.is_valid():
            full_name = form.cleaned_data.get("full_name")
            user = form.save(commit=False)
            user.set_password(form.cleaned_data.get("password"))
            user.first_name = full_name.split()[0]
            user.last_name = full_name.split()[1]
            user.is_active = False
            user.save()
            email_verification.delay(user_id=user.pk, )
            context["message"] = "Please check your inbox !"
            return render(request, "accounts/login.html", context)
        else:
            context["login_form"] = form
            return render(request, "accounts/login.html", context)
    else:
        return redirect("home")
Exemplo n.º 3
0
def register_view(request):
    if request.method == "POST":
        context = login_page_data()
        form = RegisterForm(request.POST, prefix="register")
        if form.is_valid():
            full_name = form.cleaned_data.get("full_name")
            user = form.save(commit=False)
            user.set_password(form.cleaned_data.get("password"))
            user.first_name = full_name.split()[0]
            user.last_name = full_name.split()[1]
            user.save()
            if user and user.is_active:
                login(request, user)
                return redirect("dashboard")
            else:
                context["message"] = "username or password invalid !"
                return render(request, "accounts/login.html", context)
        else:
            context["login_form"] = form
            return render(request, "accounts/login.html", context)
    else:
        return redirect("home")
Exemplo n.º 4
0
 def get(self, request):
     form = RegisterForm()
     return render(request, 'register.html', {'form': form})
Exemplo n.º 5
0
def login_page_data():
    return {
        "login_form": LoginForm(),
        "register_form": RegisterForm(prefix="register"),
    }