Beispiel #1
0
def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')

    if request.method == 'POST':
        # 1. 表单验证
        form = UserLoginForm(request.POST)
        # 使用is_valid()进行表单验证
        if form.is_valid():
            # form表单验证成功
            user = auth.authenticate(username=form.cleaned_data['username'],
                                     password=form.cleaned_data['password'])
            if user:
                # 如果通过username和password获取到user对象,则进行登录
                # request.user默认AnonyMouseUser
                auth.login(request, user)
                return HttpResponseRedirect(reverse('home:index'))
            else:
                # 用户名和密码错误
                return render(request, 'login.html')
        # 2. auth模块验证
        # 3. auth.login登录
        else:
            # form验证失败,则返回错误信息到页面
            return render(request, 'login.html', {'form': form})
Beispiel #2
0
def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')

    if request.method == 'POST':
        # 1.表单验证
        form = UserLoginForm(request.POST)
        # 使用is_valid()进行表单验证
        # 2.auth模块验证
        # 3.auth.login登录
        if form.is_valid():
            # form表单验证成功
            user = auth.authenticate(username=form.cleaned_data['username'],
                                     password=form.cleaned_data['password'])
            if user:
                # 如果通过auth拿到对象,就进行登录
                # request.user默认AnonyMouseUser改为user
                auth.login(request, user)
                return HttpResponseRedirect(reverse('home:index'))
            else:
                # auth验证失败,返回信息到页面
                msg = '用户不存在或密码不正确'
                return render(request, 'login.html', {'msg': msg})
        else:
            return render(request, 'login.html', {'form': form})
Beispiel #3
0
def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')

    if request.method == 'POST':
        # 1.表单验证
        form = UserLoginForm(request.POST)
        # 使用is_valid()进行表单验证
        if form.is_valid():
            # form表单验证成功
            user = auth.authenticate(
                username=form.cleaned_data.get('username'),
                password=form.cleaned_data.get('password'),
            )
            # 2.auth模块验证
            # 3.auth.login登录
            if user:
                # 如果用户名密码正确获取到user对象,则进行登录
                # request.user默认是AnonyMouseUser
                auth.login(request, user)  # 实质上是给request.user赋值
                return HttpResponseRedirect(reverse('home:index'))
            else:
                # 获取用户名和密码错误
                return render(request, 'login.html', {'error': '密码错误'})

        else:
            # form验证失败,则将错误返回
            return render(request, 'login.html', {'error': form})
Beispiel #4
0
def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    if request.method == 'POST':
        form = UserLoginForm(request.POST)
        if form.is_valid():
            # 验证成功
            user = auth.authenticate(username=form.cleaned_data['username'],
                                     password=form.cleaned_data['password'])
            if user:
                auth.login(request, user)
                return HttpResponseRedirect(reverse('home:index'))
            else:
                return render(request, 'login.html')
        else:
            return render(request, 'login.html', {'form': form})
Beispiel #5
0
def login_user(request):
    if request.user.is_authenticated():
        HttpResponseRedirect('/store/')
        
    error = None
    
    if request.method == 'POST':
        form = UserLoginForm(request.POST)
        
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']

            user = authenticate(email=email, password=password)
            
            if user is not None:
                
                if settings.DOWN_FOR_MAINTENANCE and not user.is_staff:
                    error = "The site is currently down for maintenance. Please try again later"
                else:
                    login(request, user)
                    
                    return HttpResponseRedirect('/')
            else:
                error = "Invalid Username or Password"
    else:
        form = UserLoginForm()
        
    return render_to_response (
            'login.html', 
            { 
                'form': form,
                'error' : error 
            },
            context_instance=RequestContext(request)
        )