Пример #1
0
def login(request):

    if request.session.get('is_login', None):  #檢查session確定是否登入,不允許重複登入
        return redirect("/index/")  #若已登入則導向主頁
    if request.method == 'POST':  #接收POST訊息,若無則讓返回空表單
        login_form = UserForm(request.POST)  #導入表單模型
        if login_form.is_valid():  #驗證表單
            username = login_form.cleaned_data[
                'username']  #從表單的cleaned_data中獲得具體值
            password = login_form.cleaned_data['password']
            try:
                user = models.User.objects.get(name=username)
                if user.password == hash_code(password):  #密文處理
                    #使用session寫入登入者資料
                    request.session['is_login'] = True
                    request.session['user_id'] = user.id
                    request.session['user_name'] = user.name
                    message = "登入成功"
                    return redirect('/index/')
                else:
                    message = "密碼不正確"
            except:
                message = "該用戶不存在"
    login_form = UserForm(request.POST)  #返回空表單
    return render(request, "blog/login.html", locals())
Пример #2
0
def zhuce_ajax(request):  # 注册ajax请求处理
    if request.method == "POST":  # 判断POST请求
        form = UserForm(request.POST)  # 将post数据传给自定义类
        result = {"state": False, "name": "", "pwd": "", "r_pwd": ""}
        if form.is_valid():
            name = request.POST.get("name")
            pwd = request.POST.get("pwd")
            # 创建普通用户
            ret = UserInfo.objects.create_user(username=name, password=pwd)
            if ret:
                result["state"] = True
            return HttpResponse(json.dumps(result, ensure_ascii=False))
        else:
            print(form.errors)
            if form.errors:  # 判断有错误信息的情况下
                if form.errors.get("name"):
                    result["name"] = form.errors.get("name")[0]
                if form.errors.get("pwd"):
                    result["pwd"] = form.errors.get("pwd")[0]
                if form.errors.get("r_pwd"):
                    result["r_pwd"] = form.errors.get("r_pwd")[0]

                g_error = form.errors.get("__all__")  # 接收全局钩子错误信息
                if g_error:  # 判断有错误信息的情况下
                    g_error = g_error[0]  # 取第一个错误信息
                    result["r_pwd"] = g_error

                return HttpResponse(json.dumps(result, ensure_ascii=False))
Пример #3
0
def zhuce(request):  # 注册
    if request.method == "POST":
        # 将post数据传给UserForm
        form = UserForm(request.POST)

    else:  # 默认是get请求(地址栏输入访问时)
        form = UserForm()  # 没有表单数据的form
    return render(request, "zhuce.html", {"form": form})
Пример #4
0
def login(req):
    if req.method == "POST":
        uf = UserForm(req.POST)
        if uf.is_valid():
            username = uf.instance.username
            password = hashlib.sha1(uf.cleaned_data['password']).hexdigest()
            try:
                User.objects.get(username=username,password=password)
                response = HttpResponseRedirect('/food/1')
                response.set_cookie('username',value=username)
                return response
            except ObjectDoesNotExist:
                return HttpResponseRedirect('/regist/')
    else:
        uf = UserForm()
    return render(req,'login.html',{'uf':uf})
Пример #5
0
def register(req):  #注册账户
    if req.method == 'POST':
        uf = UserForm(req.POST)
        if uf.is_valid():
            #获得表单数据
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            #添加到数据库
            User.objects.create(username=username, password=password)
            return HttpResponseRedirect('/login/')  #注册成功重定向至登陆页面
    else:
        uf = UserForm()
    return render(
        req,
        'register.html',
        {'uf': uf},
    )
Пример #6
0
def register(req):
    if req.method=="POST":
        uf = UserForm(req.POST,req.FILES)
        if uf.is_valid():
            data = uf.cleaned_data
            data['password'] = hashlib.md5(data['password']).hexdigest()
            print data
            user = User()
            user.username = data['username']
            user.password = data['password']
            user.email = data['email']
            user.birthday = data['birthday']
            user.headimg = data['headimg']
            user.desc = data['desc']
            user.sex = data['sex']
            user.save()
            return HttpResponse('ok')
    else:
        uf = UserForm()
    return render(req,'register.html',{"uf":uf})
Пример #7
0
def register(request):
    registered = False
    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_picture' in request.FILES:
                profile_picture = request.FILES['profile_picture']
            profile.save()
            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    return render(
        request, 'register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Пример #8
0
def register(request):
    registered=False
    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save() 
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_picture' in request.FILES:
                profile_picture = request.FILES['profile_picture']
            profile.save()
            registered = True
        else:
            print (user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    return render(request, 
        'register.html',
  	    {'user_form' : user_form, 'profile_form' : profile_form, 'registered' : registered} )
Пример #9
0
def regist(req):
    if req.method == "POST":
        uf = UserForm(req.POST)
        if uf.is_valid():
            try:
                User.objects.get(username=uf.instance.username)
                uf = UserForm()
                return render(req,'regist.html',{'uf':uf})
            except:
                uf.instance.password = hashlib.sha1(uf.cleaned_data['password']).hexdigest()
                uf.save()
                return HttpResponseRedirect('/login/')
    else:
        uf = UserForm()
    return render(req,'regist.html',{'uf':uf})
Пример #10
0
def login(req):  #登陆账户
    if req.method == 'POST':
        uf = UserForm(req.POST)
        if uf.is_valid():
            #获取表单用户密码
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            #获取的表单数据与数据库进行比较
            user = User.objects.filter(username__exact=username,
                                       password__exact=password)
            if user:
                #比较成功,跳转blog主页
                response = HttpResponseRedirect('/blog/')
                #将username写入浏览器cookie,失效时间为3600
                response.set_cookie('username', username, 3600)
                return response
            else:
                #比较失败,还在login
                uf = UserForm()
                render(req, 'login.html', {'uf': uf})
    else:
        uf = UserForm()
    return render(req, 'login.html', {'uf': uf})