登录后获取request.user作为超级用户名

时间:2018-07-04 07:47:40

标签: django-views

i have a question when i am login i am getting the correct request.user in user_login

但是在deal_form视图中request.user是不同的,我得到的用户是超级用户名,但是我想要登录用户名,为什么会这样呢?请您纠正我的代码,我的代码有什么问题。下面是我的views.py

def user_login(request):
    if request.method == 'POST':
        # First get the username and password supplied
        username = request.POST.get('username')
        password = request.POST.get('password')
        # Django's built-in authentication function:
        request.user = authenticate(username=username, password=password)

        # If we have a user
        if request.user:
            #Check it the account is active
             if request.user.is_active:
                # Log the user in.
                login(request)
                print('login')
                print(request.user)
                #type_user = request.user.groups.all()
                if request.user.groups.filter(name='Client').exists():
                    # Send the user back to some page.
                    # In this case their homepage.
                    return HttpResponseRedirect('/client')
                elif request.user.groups.filter(name='Advertizer').exists():
                    # If account is not active:
                    return HttpResponseRedirect("/first_app/deal_form")
                # Send the user back to some page.
                # In this case their homepage.
                #else:
                    #return render(request,'admin.html')

        else:
            print("Someone tried to login and failed.")
            print("They used username: {} and password: {}".format(username,password))
            return HttpResponse("Invalid login details supplied.")

    else:
        #Nothing has been provided for username or password.
        return render(request, 'login.html', {})

def deal_form(request):
    if request.user is None:
        return HttpResponseRedirect('/first_app/user_login')
    saved = False
    person=request.user
    print(person)
    categories = DealCategory.objects.all()
    print(categories)
    if request.method == 'POST':
        form = DealForm(request.POST,request.FILES)
        print(form.errors)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/first_app/deal_form")
    return render(request,'advertizer/adver_create_coupon.html', {'categories': categories, 'person': person})

0 个答案:

没有答案