Esempio n. 1
0
def user_login(request):
    """用户登陆页面"""
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        if form.is_valid():
            form.do_login(request)
            print('表单信息验证成功')
            return redirect('/accounts/user/info/')
        else:
            print(form.errors)
    else:
        form = LoginForm()
    return render(request, 'user_login.html', {'form': form})
Esempio n. 2
0
def user_api_login(request):
    """用户登陆接口"""
    # 1.获取用户输入信息
    if request.method == 'POST':
        # 2.表单验证
        form = LoginForm(data=request.POST)
        print(form)
        # 3.如果表单验证通过,执行登陆
        if form.is_valid():

            user = form.do_login(request)
            profile = user.profile
            # 4.返回用户的基本信息和详细信息
            data = {
                'user': UserSerializer(user).to_dict(),
                'profile': UserProfileSerializer(profile).to_dict()
            }
            return http.JsonResponse(data)
        # 5.如果表单验证不通过,返回用户登陆的错误信息
        else:
            err = json.loads(form.errors.as_json())
            return BadJsonResponse(err)
    # 6.如果当前的请求不是post而是get 则返回获取方式错误的信息
    else:
        return MethodNotAllowResponse()
Esempio n. 3
0
def user_api_login(request):
    """ 用户登录接口-POST """
    # 获取输入的内容
    if request.method == 'POST':
        # 表单验证
        form = LoginForm(request.POST)
        # 如果通过了验证,执行登录
        if form.is_valid():
            user = form.do_login(request)
            # 返回内容:用户的信息(用户的基本信息、详细信息)
            profile = user.profile  # 1对1的反向查询,返回一个profile对象
            data = {
                'user': serializers.UserSerializer(user).to_dict(),
                'profile':
                serializers.UserProfileSerializer(profile).to_dict()
            }
            return http.JsonResponse(data)
            # return HttpResponse("oK")
        else:
            # 如果没有通过表单验证,返回表单的错误信息
            err = json.loads(form.errors.as_json(
            ))  # form.errors.as_json()就是一个json,通过.loads转换成python对象
            return BadRequestJsonResponse(
                err)  # 因为这个函数里要放入python对象,err里装的就是form里raise抛出的错误信息
    else:
        # 请求不被允许
        return MethodNotAllowedJsonResponse()
Esempio n. 4
0
def user_login(request):
    """ 用户登录 """
    if request.method == 'POST':
        # Form表单默认属性顺序(self, data=None, files=None, auto_id='id_%s', prefix=None,
        #                  initial=None, error_class=ErrorList, label_suffix=None,
        #                  empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None):
        form = LoginForm(data=request.POST)
        if form.is_valid():
            form.do_login(request)
            print('表单验证通过')
            return redirect('/accounts/user/info/')
        else:
            print(form.errors)
    else:
        form = LoginForm()
    return render(request, 'user_login.html', {'form': form})
Esempio n. 5
0
def login():
    """ 登录页面 """
    form = LoginForm()
    next_url = request.values.get('next', url_for('qa.index'))
    if form.validate_on_submit():
        user = form.do_login()
        if user:
            # 4. 跳转到首页
            flash('{}, 欢迎回来'.format(user.nickname), 'success')
            return redirect(next_url)
        else:
            flash('登录失败,请稍后重试', 'danger')
    # else:
    #     print(form.errors)
    return render_template('login.html', form=form, next_url=next_url)
Esempio n. 6
0
def login():
    form = LoginForm()
    next_url = request.values.get('next', url_for('qa.index'))
    if form.validate_on_submit():
        user = form.do_login()
        if user:
            # 4 跳转到首页
            flash('{}欢迎回来'.format(user.nickname), 'success')
            return redirect(next_url)
        else:
            flash('登陆失败', 'danger')

    # else:
    #     print(form.errors)
    #     print(form.username.data)
    #     print(form.password.data)
    return render_template('login.html', form=form, next_url=next_url)
Esempio n. 7
0
def user_api_login(request):
    """ 用户登录接口——POST """
    # 获取输入内容
    if request.method == 'POST':
        # 表单验证
        form = LoginForm(request.POST)
        # 如果通过验证,执行登录
        if form.is_valid():
            user = form.do_login(request=request)
            # 返回内容:用户信息
            profile = user.profile
            data = {
                'user': UserSerializer(obj=user).to_dict(),
                'profile': UserProfileSerializer(obj=profile).to_dict(),
            }
            return http.JsonResponse(data=data)
        else:
            # 如果未通过表单验证,返回错误信息
            err = json.loads(form.errors.as_json())
            return BadRequestJsonResponse(err)
    else:
        # 请求方式不被允许
        return MethodNotAllowedJsonResponse()