Ejemplo n.º 1
0
def login(request):
    if request.user.is_authenticated():
        return redirect('/')
    user = request.user.username
    if request.method == 'POST':    
        login_form = forms.LoginForm(request.POST)
    #    form = authentication_form(request, data=request.POST)
        if login_form.is_valid():
            login_name=request.POST['username']
            login_password=request.POST['password']
            user = authenticate(username=login_name, password=login_password)
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    messages.add_message(request, messages.SUCCESS, '成功登入了')
                    return redirect('/')
                else:
                    messages.add_message(request, messages.WARNING, '帳號尚未啟用')
            else:
                messages.add_message(request, messages.WARNING, '登入失敗')
        else:
            messages.add_message(request, messages.INFO,'請檢查輸入的欄位內容')
    else:
         login_form = forms.LoginForm() 
         user = ""
         return render_to_response('login.html')
        

    context = { "user": user }
    return render(request, "login.html", context)

    template = get_template('login.html')
    request_context = RequestContext(request)
    request_context.push(locals())
    html = template.render(locals())
Ejemplo n.º 2
0
def login(request):
    if request.user.is_authenticated():
        return redirect('/')
    if request.method == 'POST':
        login_form = forms.LoginForm(request.POST)
        if login_form.is_valid():
            login_name = request.POST['username'].strip()
            login_password = request.POST['password']
            user = authenticate(username=login_name, password=login_password)
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    messages.add_message(request, messages.SUCCESS,
                                         "Succed!!!")
                    return redirect('/')
                else:
                    messages.add_message(request, messages.WARNING,
                                         'no activate')
            else:
                messages.add_message(request, messages.WARNING, 'fail')
        else:
            messages.add_message(request, messages.INFO, 'examine content')
    else:
        login_form = forms.LoginForm()

    template = get_template('login.html')
    request_context = RequestContext(request)
    request_context.push(locals())
    #html = template.render(request_context)
    return render(request, template, request_context)
Ejemplo n.º 3
0
def login3(request):
    if (request.method == "POST"):
        login_form = forms.LoginForm(request.POST)
        if (login_form.is_valid()):
            login_name = request.POST['user_name'].strip()
            login_password = request.POST['password']
            user = auth.authenticate(username=login_name, password=login_password)
            if(user is not None):
                if(user.is_active):
                    auth.login(request, user)
                    print("success login")
                    messages.add_message(request, messages.SUCCESS, "登陆成功!")
                    return redirect("/")
                else:
                    messages.add_message(request, messages.WARNING, "该账号尚未启用!")
            else:
                messages.add_message(request, messages.WARNING, "登录失败")
        else:
            messages.add_message(request, messages.WARNING, "请检查输入字段内容")

    else:
        login_form = forms.LoginForm()
    #login_form.user_name.label
    mymsg = messages.get_messages(request)
    template = get_template("login2.html")
    c = csrf(request)
    c.update(locals())
    html = template.render(c)
    # response = HttpResponse(html)

    return HttpResponse(html)
Ejemplo n.º 4
0
def login(request):
    if request.method == 'POST':  # 检查进来的请求是否是“POST”
        login_form = forms.LoginForm(request.POST)  # 如果是,实例化登录表单(POST)
        if login_form.is_valid():  # 如果表单的内容合理有效
            login_name = request.POST['username'].strip(
            )  # 提交的用户名赋值给login_name
            login_password = request.POST['password']  # 提交的密码赋值给login_password

            user = authenticate(username=login_name,
                                password=login_password)  # 试着在系统自动的User表中进行验证
            if user is not None:  # 如果找到了
                if user.is_active:
                    auth.login(request, user)  # 将此用户存入session中
                    messages.add_message(request, messages.SUCCESS, '登录成功了')
                    return redirect('/')  # 转向根目录网址
                else:
                    messages.add_message(request, messages.WARNING, '账号未启用')
            else:
                messages.add_message(request, messages.WARNING,
                                     '登录失败')  # 在User数据表中找不到用户
        else:
            messages.add_message(request, messages.INFO, '请检查输入的字段内容')
    else:
        login_form = forms.LoginForm()  # 实例化非POST表单
    return render(request, 'login.html', locals())  # 返回数值,渲染网页
Ejemplo n.º 5
0
def login(request):
    TheTimeOfNow = datetime.now()
    if request.user.is_authenticated:
        username = request.user.username
        messages.add_message(request, messages.INFO, "您已登录")
        return redirect('/')
    if request.method == 'POST':
        login_form = forms.LoginForm(request.POST)
        if login_form.is_valid():
            login_name = request.POST['username'].strip()
            login_password = request.POST['password']
            user = authenticate(username = login_name, password = login_password)
            if user is not None:
                if user.is_active:
                    auth.login(request,user)
                    messages.add_message(request,messages.SUCCESS,"成功登录了")
                    return redirect('/')
                else:
                    messages.add_message(request,messages.WARNING,"账户未启用")
            else:
                messages.add_message(request, messages.WARNING,"登录失败")
        else:
            messages.add_message(request, messages.INFO, "请检查输入字段的内容")
    else:
        login_form = forms.LoginForm()
    return render(request,'login.html',locals())
Ejemplo n.º 6
0
def login(request):
    form = forms.LoginForm()
    error = ""
    if request.method=='POST':
            form = forms.LoginForm(request.POST)
            if form.is_valid():
                    user = authenticate(username=form.cleaned_data['username'],password=form.cleaned_data['password'])
                    if user is not None:
                            LOGIN(request,user)
                            return HttpResponseRedirect('/')
                    else:
                            error = "Invalid Password or Username"
            else:
                    error = "Please input valid credentials"
    return render(request,"UserLogin.html",{"form":form,"error":error})
Ejemplo n.º 7
0
def register(request):
    TheTimeOfNow = datetime.now()
    if request.user.is_authenticated:
        username = request.user.username
        messages.add_message(request, messages.INFO, "您已登录")
        return redirect('/')
    else:
        if request.method == 'POST':
            register_form = forms.RegisterForm(request.POST)
            if register_form.is_valid():
                register_name = request.POST['username'].strip()
                register_password = request.POST['password']
                confirm_password = request.POST['confirm_password']
                paypassword = request.POST['paypassword']
                paypassword_confirm = request.POST['paypassword_confirm']
                if confirm_password == register_password and paypassword == paypassword_confirm:
                    user_exist = User.objects.filter(username = register_name).exists()
                    if user_exist:
                        messages.add_message(request, messages.WARNING, "此用户名已被注册")
                    else:
                        current_user = User.objects.create_user(register_name, '', register_password)
                        models.Profile.objects.create(user = current_user, id = current_user.id, paypassword = paypassword)
                        messages.add_message(request, messages.SUCCESS, "注册成功")
                        return redirect('/')
                else:
                    messages.add_message(request, messages.WARNING, "两次输入的密码不符")
            else:
                messages.add_message(request, messages.INFO, "请检查输入字段的内容")
        else:
            login_form = forms.LoginForm()
        return render(request,'register.html',locals())
Ejemplo n.º 8
0
def login(request):
    username = 0
    flag = False
    if 'username' in request.session:
        flag = True
        username = request.session['username']
    if request.method == 'POST':
        login_form = forms.LoginForm(request.POST)
        if login_form.is_valid():
            username = request.POST['user_name']
            userpass = request.POST['user_password']
            message = "登录成功" + username
            try:
                user = User.objects.get(name=username)
                if user.password == userpass:
                    flag = True
                    response = redirect('/')
                    request.session['username'] = user.name
                    request.session['useremail'] = user.email
                    return response
                else:
                    message = "wrong password"
            except:
                message = "error"
        else:
            message = "请检查"


#		username=request.POST['user_name']
#		userpass=request.POST['user_password']
#		message=username
    else:
        login_form = forms.LoginForm()
    template = get_template('login.html')
    #	if 'username' in request.COOKIES:
    #		username = request.COOKIES['username']
    response = HttpResponse(render(request, 'login.html', locals()))
    #	try:
    #		if username : response.set_cookie('username',username)
    #		if userpass: response.set_cookie('userpass',userpass)
    #	except:
    #		pass
    return response
Ejemplo n.º 9
0
def login(request):
    if request.session.get('is_login', None):
        return redirect('/index')
    message = "Begin"
    loginFailMessage = 0
    user = None
    if request.method == 'POST':
        form = forms.LoginForm(request.POST)
    else:
        form = forms.LoginForm()
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        try:
            user = models.User.objects.get(name=username)
            if user.password == password:
                request.session['is_login'] = True
                request.session['user_name'] = user.name
                request.session['user_id'] = user.pk
                request.session['user_password'] = user.password
                request.session['user_email'] = user.email
                group = Group.objects.get(name='teacher')
                try:
                    group.user_set.get(username=username)
                    request.session['is_teacher'] = True
                except:
                    pass
                return redirect('/index/')
            else:
                message = "密码不正确!"
                loginFailMessage = 1
        except:
            message = "用户不存在!"
            loginFailMessage = 1
    else:
        message = "用户信息获取失败"
        loginFailMessage = 1
    userform = forms.LoginForm(request.POST)
    template = get_template('index.html')
    html = template.render(locals())
    return HttpResponse(html)
Ejemplo n.º 10
0
def login(request):
    if request.method == 'POST':
        login_form = forms.LoginForm(request.POST)
        if login_form.is_valid():
            login_name = request.POST['username'].strip()
            login_password = request.POST['password']
            user = authenticate(username=login_name, password=login_password)
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    messages.add_message(request, messages.SUCCESS, '成功登入')
                    return redirect('/')
                else:
                    messages.add_message(request, messages.WARNING, '帳號尚未啟用')
            else:
                messages.add_message(request, messages.WARNING, '登入失敗')
        else:
            messages.add_message(request, messages.INFO, '請檢查內容')
    else:
        login_form = forms.LoginForm()

    return render(request, 'login.html', locals())
Ejemplo n.º 11
0
def login(request):
    if request.method == 'POST':
        form = forms.LoginForm(request.POST)
        if form.is_valid():
            acc = form.cleaned_data['acc']
            pwd = form.cleaned_data['pwd']
            user1 = authenticate(username=acc, password=pwd)  # 驗證是否是會員
            if user1 is not None:  # 驗證通過
                isMember = True
            else:
                isMember = False
            message = '登入成功!'
        else:
            message = '登入失敗!'
    else:
        form = forms.LoginForm()

    try:
        if acc:
            request.session['acc'] = acc
    except:
        pass
    return render(request, 'member/login.html', locals())
Ejemplo n.º 12
0
def login(request):
    if request.user.is_authenticated:
        return redirect(to='summary')
    if request.method != 'POST':
        form = forms.LoginForm()
    else:
        form = forms.LoginForm(request.POST)
        if form.is_valid():
            username = request.POST['username'].strip()
            password = request.POST['password']
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    messages.add_message(request, messages.SUCCESS, 'ログインしました')
                    return redirect(to=summary)
                else:
                    messages.add_message(request, messages.ERROR,
                                         'ユーザーは無効です。 システム管理者に連絡してください。')
            else:
                messages.add_message(request, messages.ERROR, 'ログインに失敗しました')

    return render(request, 'login.html', locals())
Ejemplo n.º 13
0
def login2(request):
    print("request login2 {0}".format(messages.get_messages(request)))
    if(request.method == "POST"):
        print("POST request")
        login_form = forms.LoginForm(request.POST)
        if(login_form.is_valid()):
            login_name = request.POST['user_name'].strip()
            login_password = request.POST['password']
            #user = auth.authenticate(username=login_name, password=login_password)
            message = "登录成功!"
            try:
                user = User.objects.get(name=login_name)
                if(user.password == login_password):
                    response = redirect("/")#####
                    request.session['username'] = user.name
                    request.session['useremail'] = user.email
                    messages.add_message(request, messages.SUCCESS, "登录成功!")
                    return response
                else:
                    message = "密码错误!"
                    messages.add_message(request, messages.WARNING, "登录失败!")
            except:
                message = "目前无法登录!"
                messages.add_message(request, messages.WARNING, "目前无法登录!")
        else:
            message = "登录失败,请检测各字段数据是否合法!"
            messages.add_message(request, messages.INFO, "登录失败,请检测各字段数据是否合法!")
    else:
        login_form = forms.LoginForm()
    mymsg = messages.get_messages(request)
    template = get_template("login2.html")
    c = csrf(request)
    c.update(locals())
    html = template.render(c)
    #response = HttpResponse(html)

    return HttpResponse(html)
Ejemplo n.º 14
0
def login(request):
    if request.method == 'POST':
        login_acc = request.POST['useracc'].strip()
        login_password = request.POST['password']
        try:
            user = models.User.objects.get(account=login_acc)
            if user.password == login_password:
                request.session['username'] = user.name
                request.session['account'] = user.account
                request.session['department'] = user.department
                request.session['email'] = user.email
                request.session['description'] = user.description
                request.session['img'] = user.img
                return redirect('/')
            else:
                messages = "密碼錯惹啦!"
        except:
            messages = "找不到使用者"
    else:
        login_form = forms.LoginForm()
    return render(request, 'index.html', locals())
Ejemplo n.º 15
0
 def login(self):
     TheTimeOfNow = self.TheTimeOfNow
     if self.request.user.is_authenticated:
         messages.add_message(self.request, messages.INFO, "您已登录")
         return redirect('/')
     if self.request.method != 'POST':
         return render(self.request, 'login.html', locals())
     login_form = forms.LoginForm(self.request.POST)
     if not login_form.is_valid():
         messages.add_message(self.request, messages.INFO, "请检查输入字段的内容")
         return redirect('/login')
     login_name = self.request.POST['username'].strip()
     login_password = self.request.POST['password']
     user = authenticate(username=login_name, password=login_password)
     if user is None:
         messages.add_message(self.request, messages.WARNING, "登录失败")
         return redirect('/login')
     if not user.is_active:
         messages.add_message(self.request, messages.WARNING, "账户未启用")
         return redirect('/login')
     auth.login(self.request, user)
     messages.add_message(self.request, messages.SUCCESS, "登录成功")
     return redirect('/')