2016-05-23 8 views
0

が、私は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」をクリックしてOKをクリックすると、クライアントが自分の動機づけの手紙を書くことができるHTMLページが表示されるので、問題はクリクシング時です[OK]をクリックすると、エラーが表示されます。禁止(403) CSRFの確認に失敗しました。リクエストが中止されました。

私は最初のビューがある変数のセッションに

を使用せずに、ステージのIDを取得するための別の解決策があるサンザシは私が行うことができますので、理由を理解haventは、あるいは:

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を復習したい

+0

共有した機能がHTMLコードの下にレンダリングされていないか、完全なビューを追加していません。何かが欠けています。あなたの完全なビューレンダリング関連のHTMLファイルを追加してください。 – alix

+0

ああ、私は今それを更新する:) – sarra

答えて

0

OK。コードをテストしませんでした。しかし、あなたがcsrfに直面しているように見えるのは、Form_resultatフォームをと同じrequestに処理した後にForm_applyフォームを送信しているためです。繰り返しますが、私はあなたのコードをテストしなかったので、私はこの解決策についてはわかりません。別のURLを作成します

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

私はあなたのコードでこれ以外に何か間違ったことは見ませんでした。だからこのアプローチを試して、それが動作するかどうかを教えてください。

関連する問題