예제 #1
0
def login(request):
    # 通过session判断用户是否登录
    if request.session.get('is_login'):
        return redirect('/api/v1.0/TGou/index')

    # POST请求
    if request.method == "POST":
        # 接收表单数据生成表单对象
        login_form = UserForm(request.POST)
        # message = '检查填写内容'
        # 使用表单类自带的is_valid()方法一步完成数据验证工作,例如:验证码
        if login_form.is_valid():
            # 验证成功,从表单对象中获取具体的值
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            print('login--------', username, password)
            users = models.User.objects.all()
            try:
                for user in users:
                    # 判断输入的是否存在于name或email中
                    if user.name == username or user.email == username:
                        if not user.has_confirmed:
                            message = "该用户还未通过邮件确认,请先去邮箱确认!"
                            return render(request, 'Login/login.html', locals())
                        if user.password == make_password(password,"TGou",hasher='pbkdf2_sha1'):
                            # 在session中添加用户状态或者信息
                            request.session['is_login'] = True
                            # request.session['user_id'] = user.id
                            request.session['user_name'] = user.name
                            return redirect('/api/v1.0/TGou/index')
                else:
                    message = '用户名或密码错误'
            except:
                message = '用户不存在'
        # 验证没通过会返回一个包含先前数据的表单给前端页面,方便用户修改
        # locals() Python内置函数,它返回当前所有的本地变量字典不用去构造字典,例如:{'message':message, 'login_form':login_form},但是同时也可能往模板传入了一些多余的变量数据,造成数据冗余降低效率。
        return render(request, 'Login/login.html', locals())
    # GET请求返回空的表单,用于用户登录
    login_form = UserForm()
    return render(request, 'Login/login.html', locals())
예제 #2
0
def register(request):

    registered = False

    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)  # hashing password
            user.save()  # saves in db

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()

            registered = True  # if all good change flag
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(
        request, 'Login/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
예제 #3
0
def parent_edit(request, id=None):
    user = get_object_or_404(User, id=id)
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=user)
        if user_form.is_valid():
            user_form.save()
            return HttpResponseRedirect(reverse('parent_list'))
        else:
            return render(request, 'parent/edit.html', {"user_form": user_form})
    else:
        user_form = UserForm(instance=user)
        return render(request, 'parent/edit.html', {"user_form": user_form})
예제 #4
0
def parent_add(request):
    context = {}
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        context['user_form'] = user_form
        if user_form.is_valid():
            u = user_form.save()
            return HttpResponseRedirect(reverse('parent_list'))
        else:
            return render(request, 'parent/add.html', context)
    else:
        user_form = UserForm()
        context['user_form'] = user_form
        return render(request, 'parent/add.html', context)
예제 #5
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
            profile.save()
            registered = True
        else:
            print(user_form.errors,profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    return render(request,'Login/registration.html', {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})