Django会话变量

时间:2016-05-23 16:39:00

标签: python django session-variables

我正在使用django和python处理Web服务,我遇到了CSRF令牌问题,这是我的HTML代码:

def apply(request):
    user = request.user
    username=user.username
    if user and user.is_active:
        if request.method == 'POST':
            print("post")
            form = Form_demande(request.POST)
            form_app = Form_apply(request.POST)
            if form.is_valid():
                candidat=CompteCandidat.objects.all().get(username=username)
                firstname = candidat.first_name
                lastname = candidat.last_name
                motivation = form.cleaned_data['motivation']
                p=form_app.cleaned_data['apply']
                idstage = request.POST.get("idstage", "")
                q1 = eStage.objects.filter(id=idstage)
                st=eStage(typeposte=q1[0].typeposte,diplome=q1[0].diplome,niveau=q1[0].niveau,duree=q1[0].duree,commentaire=q1[0].commentaire,compteEntr=q1[0].compteEntr)
                st.save()
                demande = Demande.objects.create(first_name=firstname, last_name=lastname, motivation=motivation, stage=st)
                demande.save()
                return render(request,'apply.html', {'form': form})
        else:
            print("cou altern")
            form = Form_demande(request.POST)
            return render(request,'apply.html', {'form': form})
    else:
        return redirect('/home')

我有这样的功能:

def get_stage_by_motcle(request):
    user=request.user
    if user and user.is_active:
        if request.method == 'POST':
            form = Form_resultat(request.POST)
            if form.is_valid():
                m = form.cleaned_data["mot"]
                mtc=motcle(motcle=m)
                mtc.save()
                query=motcle.objects.all().filter(motcle=mtc)
                queryset=eStage.objects.all().filter(mot=query)
                form_app = Form_apply()
                return render_to_response('resultat_by_mot.html', {'resultat': queryset,'form_app':form_app})
            else:
                form = Form_resultat(request.POST)
                return render(request, 'get_by_mot.html', {'form': form})
        else:
            form = Form_resultat(request.POST)
            return render(request,'get_by_mot.html',{'form': form})
    else:
        return redirect('/home')

我想要显示'阶段'当客户穿上' Je postule'然后在OK上它会显示一个html页面,客户端可以在其中写下他的动机信,所以问题是当他cliks on OK它会显示一个错误:Forbidden(403) CSRF验证失败。请求中止。

我不明白为什么,所以我可以这么做,或者有另一个解决方案来获取舞台的id而不使用变量会话

第一个观点是:

{{1}}

它是一个显示每个阶段属性的函数,我想恢复每个阶段的id,以便在'中使用它。申请'功能

1 个答案:

答案 0 :(得分:0)

行。没有测试你的代码。但看起来您遇到了csrf错误,因为您在Form_applyForm_resultat处理same view表单后发送了request表单。再次,由于我没有测试你的代码,我不确定这个解决方案。但值得尝试:

创建另一个网址:

# other urls...
url(r'^form-apply/(?P<mtc_id>[0-9]+)', 'form_apply_view', name='form_apply_view'),

并创建相关视图:

def form_apply_view(request, mtc_id):
    query=motcle.objects.all().filter(pk=mtc_id)
    queryset=eStage.objects.all().filter(mot=query)
    form_app = Form_apply()
    return render_to_response('resultat_by_mot.html', {'resultat': queryset,'form_app':form_app})

您的新get_stage_by_moctle视图可能如下:

def get_stage_by_motcle(request):
    user=request.user
    if user and user.is_active:
        if request.method == 'POST':
            form = Form_resultat(request.POST)
            if form.is_valid():
                m = form.cleaned_data["mot"]
                mtc=motcle(motcle=m)
                mtc.save()
                # redirect to your new url to render new form with saved object data...
                return redirect('form_apply_view', mtc_id=mtc.id)
            else:
                form = Form_resultat(request.POST)
                return render(request, 'get_by_mot.html', {'form': form})
        else:
            form = Form_resultat(request.POST)
            return render(request,'get_by_mot.html',{'form': form})
    else:
        return redirect('/home')

除此之外,我的代码中没有看到任何错误。所以尝试这种方法,让我知道它是否有效。