2016-05-23 87 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') 

除此之外,我在代碼中看不到任何錯誤。所以試試這種方法,讓我知道它是否有效。