2016-05-23 86 views
0

我使用Django和Python的Web服务上的工作,我有CSRF令牌的问题,这是我的html代码:Django的会话变量

  {% for f in resultat %} 
        <div class="box"> 


         <BR> 
         <div class="row uniform 50%"> 
          <div class="6u 12u(mobilep)"> 
           {{ f.typeposte }} 
          </div> 
         </div> 
         <BR> 
         <div class="row uniform 50%"> 
          <div class="6u 12u(mobilep)"> 
           {{ f.diplome }} 
          </div> 
         </div> 
         <BR> 
         <div class="row uniform 50%"> 
          <div class="6u 12u(mobilep)"> 
           {{ f.niveau }} 
          </div> 
         </div> 
         <BR> 
         <div class="row uniform 50%"> 
          <div class="6u 12u(mobilep)"> 
           {{ f.duree }} 
          </div> 
         </div> 
         <BR> 
         <div class="row uniform 50%"> 
          <div class="6u 12u(mobilep)"> 
           {{ f.commentaire }} 
          </div> 
         </div> 
       <div class="box"> 
       <form class="form_app" action="/apply" method="post"> 
       {% csrf_token %} 
       <div class="row uniform 50%"> 
        <div class="6u 12u(mobilep)"> 
         {{form_app.apply}} 
        </div> 
        <input type="hidden" name="title" value="ouf"> 
       </div> 
       <div class="row uniform"> 
        <div class="12u"> 
         <ul class="actions align-center"> 
          <li><input type="submit" value="OK"/></li> 
         </ul> 
        </div> 
       </div> 
       </form> 
      </div> 

,我有这样的功能:

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') 

我想要显示的'阶段',当客户端在'Je postule'上cl然后在OK上插入一个html页面,客户可以在其中写出他的动机信,所以问题是他何时碰到确定它显示我一个错误:禁止(403) CSRF验证失败。请求中止。

我还没有明白为什么,所以我山楂可以做,或者出现一个又一个解决方案,使舞台的ID,而无需使用可变会话

第一个观点是:

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') 

其功能谁显示每个阶段的属性,我想恢复每个阶段的id以便在'apply'函数中使用它

+0

您分享的功能不是视图在HTML代码下呈现,或者您未添加完整视图。缺了点什么。请添加您的完整视图呈现相关的html文件。 – alix

+0

噢,我现在更新它:) – sarra

回答

0

OK。没有测试你的代码。但看起来像你面临的csrf错误,因为你在发送Form_apply表格后处理Form_resultat表格same viewrequest。再次,因为我没有测试你的代码,我不确定这个解决方案。但它是值得一试:

创建另一个网址:

# 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') 

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