Esempio n. 1
0
def login(request, template_name='signin.html'):
    if request.GET.get('next', None):
        request.session['POST_AUTH_URL'] = request.GET.get('next')
    if 'POST' == request.method:
        form = LoginForm(data=request.POST)
        if form.is_valid():
            auth.login(request, form.get_user())
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            if form.cleaned_data['keep_signed_in']:
                # Sessions expire in about ten years.
                request.session.set_expiry(datetime.timedelta(3650))
            else:
                # Session expires when user closes browser.
                request.session.set_expiry(0)
            if request.GET.get('ajax'):
                pass
                '''
                from utils import json
                return HttpResponse(
                    json.get_json(
                        request,
                        200,
                        ugettext(u'Login successful.'),
                        dict()
                    ),
                    content_type=json.get_response_mimetype(request)
                )
                '''
            else:
                return accounts.post_auth_redirect(request)
        else:
            if request.GET.get('ajax'):
                pass
                '''
                from utils import json
                return HttpResponse(
                    json.get_json(
                        request,
                        403,
                        ugettext(u'Authentication Failed. Access forbidden.'),
                        dict()
                    ),
                    content_type=json.get_response_mimetype(request)
                )
                '''
    else:
        form = LoginForm()

    context = dict(
        form=form
    )

    return render_to_response(
        template_name,
        context,
        context_instance=RequestContext(request)
    )
Esempio n. 2
0
def login(request):
    logout(request)
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            auth_login(request, form.get_user())
            return redirect(request.GET.get('next', 'home'))
    else:
        form = LoginForm()

    return render(request, 'accounts/login.html', {'form': form})
Esempio n. 3
0
	def post(self, request):
		form = LoginForm(request.POST)
		if form.is_valid():
			user = form.get_user()
			if not user:
				return redirect('login')
			login(request, user)
			return redirect('dashboard:home')
		else:
			context = {'form': form}
			return render(request, 'dashboard/utility/login.html', context)
Esempio n. 4
0
def loginView(request):
    form = LoginForm()

    if request.POST:
        form = LoginForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return redirect("home page")

    return render(request, 'login.html', {"loginform": form})
Esempio n. 5
0
def login_view(request):
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        if form.is_valid():
            #login the user
            user = form.get_user()
            login(request, user)
            return redirect('articles:list')
    else:
        form = LoginForm()

    return render(request, 'accounts/login.html', {'form': form})
Esempio n. 6
0
def login_view(request):
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            if 'next' in request.POST:
                return redirect(request.POST.get('next'))
            else:
                return redirect('home')
    else:
        form = LoginForm()
    return render(request, 'accounts/login.html', {'form': form})
def sign_in_view(request):
    if (request.method == 'POST'):
        form = LoginForm(data=request.POST)
        if form.is_valid():
            #log in the user
            user = form.get_user()
            login(request, user)
            if ('next' in request.POST):
                return redirect(request.POST.get('next'))
            else:
                return redirect('/')
    else:
        form = LoginForm()
    return render(request, 'accounts/login.html', {'form': form})
Esempio n. 8
0
def login(request):
    if request.method == 'POST':
        loginform = LoginForm(request, request.POST)
        if loginform.is_valid():
            user = loginform.get_user()
            if user is not None:
                auth_login(request, user)
                return HttpResponseRedirect(reverse('index'))
            print('bb')
        print('cc')
    else:
        loginform = LoginForm()
        print('aa')
        user = None
    return render(request, 'account/login.html', {'form': loginform})
Esempio n. 9
0
def login_page(request):
    if request.method == 'POST':
        redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '/')
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            login(request, login_form.get_user())
            print redirect_to
            return HttpResponseRedirect(redirect_to)
    login_form = LoginForm()
    return render_to_response("login_page.html", context_instance=RequestContext(
                request, {
                    "action": "login",
                    "form": login_form,
                    "errors": login_form.errors.values()
                }))
Esempio n. 10
0
def login_page(request):
    if request.method == 'POST':
        redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '/')
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            login(request, login_form.get_user())
            print redirect_to
            return HttpResponseRedirect(redirect_to)
    login_form = LoginForm()
    return render_to_response("login_page.html",
                              context_instance=RequestContext(
                                  request, {
                                      "action": "login",
                                      "form": login_form,
                                      "errors": login_form.errors.values()
                                  }))
Esempio n. 11
0
 def post(self, request):
     form = LoginForm(data=request.POST)
     if form.is_valid():
         user = form.get_user()
         remember_me = form.data.get('remember_me', 'off')
         if remember_me == 'on':
             request.session.set_expiry(0)
         login(request, user)
         data = {'message': 'Success'}
         response = JsonResponse(data)
         response.status_code = 200
         return response
     else:
         data = form.errors
         response = JsonResponse(data, safe=False)
         response.status_code = 400
         return response
Esempio n. 12
0
def login(request):
    if request.method == 'POST':
        # 로그인 성공 후 이동할 URL. 주어지지 않으면 None
        next = request.GET.get('next')

        form = LoginForm(request=request, data=request.POST)
        if form.is_valid():
            user = form.get_user()
            # Django의 auth앱에서 제공하는 login함수를 실행해 앞으로의 요청/응답에 세션을 유지한다
            django_login(request, user)
            # next가 존재하면 해당 위치로, 없으면 Post목록 화면으로 이동
            return redirect(next if next else 'profile')
        # 인증에 실패하면 login_form에 non_field_error를 추가한다
        form.add_error(None, '아이디 또는 비밀번호가 올바르지 않습니다')
    else:
        form = LoginForm()
    return render(request, 'accounts/login_form.html', {'form': form})
Esempio n. 13
0
    def post(self, *args, **kwargs):
        data = self.request.POST
        form = LoginForm(data=data)

        # Calls the validation method of
        # `django.contrib.auth.forms.AuthenticationForm`
        if form.is_valid():
            # `form.get_user` is a method from
            # `django.contrib.auth.forms.AuthenticationForm`
            login(self.request, form.get_user())

            # Redirect to dashboard
            return HttpResponseRedirect(reverse('dashboard'))
        else:
            # form is not valid meaning that username/password is invalid
            # or user doesn't not exists in the database.
            self.context['form'] = form
            return render(self.request, self.template_name, self.context)
    def middleware(request):
        if request.method == "POST" and 'login' in request.POST:
            login_form = LoginForm(request, data=request.POST, prefix="login")
            if login_form.is_valid():
                login(request, login_form.get_user())
                return redirect(request.path)
            else:
                request.show_login = True
        else:
            login_form = LoginForm(request, prefix="login")
        request.login_form = login_form

        response = get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response
Esempio n. 15
0
def login_view(request):
    msg = ""
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            if 'next' in request.POST:
                return redirect(request.POST.get('next'))
            else:
                return redirect('USER_DETAILS:userprofile')
        else:
            msg = "LOGIN FAILED! TRY AGAIN"
    else:
        form = LoginForm()
    return render(request, 'accounts/login2.html', {
        'loginform': form,
        'msg': msg
    })
Esempio n. 16
0
def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/')

    if request.method == 'GET':
        next = request.path
        if next == settings.LOGIN_URL:
            next = '/'
        return render_to_response('accounts/login.html', {'next': next})

    if request.method == "POST":
        form = LoginForm(request, data=request.POST)
        if form.is_valid():
            auth.login(request, form.get_user())
            return HttpResponseRedirect(request.POST['next'])
        else:
            error = form.errors
    else:
        form = LoginForm(request)
    data = {'request': request, 'form': form, 'error': error}
    return render_to_response('accounts/login.html', data)
Esempio n. 17
0
def login(request, template_name='signin.html'):
    if request.GET.get('next', None):
        request.session['POST_AUTH_URL'] = request.GET.get('next')
    if 'POST' == request.method:
        form = LoginForm(data=request.POST)
        if form.is_valid():
            auth.login(request, form.get_user())
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            if form.cleaned_data['keep_signed_in']:
                # Sessions expire in about ten years.
                request.session.set_expiry(datetime.timedelta(3650))
            else:
                # Session expires when user closes browser.
                request.session.set_expiry(0)
            if request.GET.get('ajax'):
                return HttpResponse(
                    json.get_json(request, 200, ugettext(u'Login successful.'),
                                  dict()),
                    content_type=json.get_response_mimetype(request))
            else:
                return accounts.post_auth_redirect(request)
        else:
            if request.GET.get('ajax'):
                return HttpResponse(
                    json.get_json(
                        request, 403,
                        ugettext(u'Authentication Failed. Access forbidden.'),
                        dict()),
                    content_type=json.get_response_mimetype(request))
    else:
        form = LoginForm()

    context = dict(form=form)

    return render_to_response(template_name,
                              context,
                              context_instance=RequestContext(request))