Django:POST后,request.user成为AnonymousUser

时间:2015-11-19 03:57:30

标签: django forms post request

我最初在验证后登录:

if request.method == 'POST':        
    form = LoginForm(data=request.POST)
    username = request.POST.get('username')
    password = request.POST.get('password')

    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            auth.login(request, user)

我可以从视图跳转到查看并检查我是否仍然使用该用户登录(request.user)。 但是,在表单(类NameForm(forms.ModelForm))POST之后,request.user变为AnonymousUser!有什么想法吗?

def name(request, job_id, tc_name, last_build):
    if request.method == 'POST':        
        form = NameForm(data=request.POST)
        ownership = request.POST.get('ownership', '')
        ...
        return HttpResponseRedirect('/fail/%r/' % job.branch_id_id)
    else:
        form = NameForm(
        initial={
            'tc_name':tc_name,
            'failure':tc_fail.failure,
             ...
            }, 
        )

    return render(request, 'fail/name.html', {'form': form, 'job_id':job_id, 'branch_id':job.branch_id_id})

1 个答案:

答案 0 :(得分:0)

您的问题name视图仅包含POST方法,但在查看您的实际代码时,我在request.session.flush()视图中找到了Name

你能否在视图的开头讲述这是做什么的?删除它将解决您的问题。