示例#1
0
def log_in(request):

    login_form = UserLoginForm(request.POST or None)

    if request.method == "POST":
        if login_form.is_valid():
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            user = authenticate(username=username, password=password)

            if user is not None:
    # the password verified for the user
                if user.is_active:
                    is_new_user = user.last_login == None
                    login(request, user)
                    if is_new_user:
                        return redirect("blog-browse")
                    else:
                        return redirect("newsfeed")
                else:
                    messages.error(request, "This account has been disabled!")
            else:
                messages.error(request, "The username and password were incorrect.", extra_tags="danger")


    context = {"login_form":login_form}
    context.update(csrf(request))
    return render(request, "login.html", context)
示例#2
0
def login(request):
    if request.method == "POST":
        form = UserLoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get("username").lower()
            password = form.cleaned_data.get("password")

            user = authenticate(username=username, password=password)
            if user:    
                login_method(request, user)
                next_url = request.GET.get("next")
                if next_url:
                    return redirect(next_url)
                else:
                    return redirect(reverse("home"))
            else:
                messages.error(request, "Invalid Username or Password")
                return redirect(reverse("authentication:login"))
        else:
            import pdb; pdb.set_trace()         # For Development Purposes

    template = "authentication/login.html"
    form = UserLoginForm()
    context = {"form": form}
    return render(request, template, context)
示例#3
0
 def post(self, request):
     form = UserLoginForm(data=request.POST)
     if form.is_valid():
         user = form.get_user()
         login(request, user)
         return redirect('home')
     messages.error(request, "Ошибка входа")
     return render(request, 'authentication/login.html', {'form': form})
示例#4
0
 def post(self, request):
     form = UserLoginForm(request.POST or None)
     nextRoute = request.GET.get('next', '/')
     if form.is_valid():
         email = form.cleaned_data.get("email")
         password = form.cleaned_data.get("password")
         user = authenticate(email=email, password=password)
         login(request, user)
         return redirect(nextRoute)
     return render(request, template_name=self.template_name, context={
         "form": form
     })
示例#5
0
    def get(self, request):
        if request.user.is_authenticated:
            return redirect('/')

        form = UserLoginForm()
        return render(request, template_name=self.template_name, context={
            "form": form
        })
示例#6
0
def login(request):
    """
    return user login
    """
    if request.user.is_authenticated:
        return redirect(reverse('home'))
    if request.method == "POST":
        login_form = UserLoginForm(request.POST)

        if login_form.is_valid():
            user = auth.authenticate(username=request.POST['username'],
                                     password=request.POST['password'])

        if user:
            auth.login(user=user, request=request)
            messages.success(request, "Successfully logged in")
            return redirect(reverse('home'))

        else:
            login_form.add_error(None,
                                 "Username and/or Password are incorrect")
    else:
        login_form = UserLoginForm()

    return render(request, "login.html", {"login_form": login_form})
示例#7
0
def login(request):
    """Return the LOGIN page"""
    print(f"REquest.path = {request.path}")
    if request.META['QUERY_STRING']:
        messages.success(request,
                         "You need to be logged in to perform that action!")
        if request.META['QUERY_STRING'] == 'next=/products/upload/':
            next_page = 'file_upload'
        else:
            # other potential nex_pages should be handled here, for now home
            next_page = 'home'
    else:
        next_page = 'home'
    if request.user.is_authenticated:
        return redirect(reverse(next_page))

    if request.method == "POST":
        login_form = UserLoginForm(request.POST)

        if login_form.is_valid():
            user = auth.authenticate(username=request.POST['username'],
                                     password=request.POST['password'])
            if user:
                auth.login(user=user, request=request)
                messages.success(request,
                                 "You have successfully been logged in.")
                return redirect(reverse(next_page))
            else:
                login_form.add_error(None,
                                     "Username or Password is incorrect!")
    else:
        login_form = UserLoginForm()
    return render(request, 'login.html', {"login_form": login_form})
示例#8
0
def test_user_form_error():
    email = "*****@*****.**"
    password = "******"
    form = UserLoginForm({"email": email, "password": password})
    assert form.is_valid() is False

    email = "test"
    password = "******"
    form = UserLoginForm({"email": email, "password": password})
    assert form.is_valid() is False
示例#9
0
 def get(self, request):
     form = UserLoginForm()
     return render(request, 'authentication/login.html', {'form': form})
示例#10
0
def test_user_form_ok():
    email = "*****@*****.**"
    password = "******"
    form = UserLoginForm({"email": email, "password": password})
    assert form.is_valid() is True