2017-02-01 5 views
2

views.pyCSRFトークンジャンゴ

from django.shortcuts import render 
from django.contrib.auth.decorators import login_required 
from django.http import HttpResponse 
import MySQLdb 
from django.shortcuts import render_to_response 
from django.shortcuts import HttpResponseRedirect 
from django.template.loader import get_template 
from django.template import Context, Template,RequestContext 
import datetime 
import hashlib 
from random import randint 
import random 
from django.views.decorators.csrf import csrf_protect, csrf_exempt 
from django.template.context_processors import csrf 
import requests 
from django.template import RequestContext 
from log.forms import * 

@csrf_protect 
def register(request): 
    if request.method == 'POST': 
     form = RegistrationForm(request.POST) 
     if form.is_valid(): 
      user = User.objects.create_user(
      username=form.cleaned_data['username'], 
      password=form.cleaned_data['password1'], 
      email=form.cleaned_data['email'] 
      ) 
      return HttpResponseRedirect('/register/success/') 
    else: 
     form = RegistrationForm() 
    variables = RequestContext(request, { 
    'form': form 
    }) 

    return render_to_response(
    'register.html', 
    variables, 
    ) 
    # return render(request,"recharge.html") 
def register_success(request): 
    return render_to_response(
    'registration/success.html', 
    ) 
base.html 
<form method="post" action="."> {% csrf_token %} 
     <table border="0"> 
       { form.as_table }} 
     </table> 
    <button type="submit" value="Register">Register</button> 
    <button type="button" onclick="window.location.href='/' ">Login</button> 
</form> 

register.html

に欠落しているか正しくありません

settings.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates')], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

私のDjangoのバージョン:1.10.4

どのように私はこの問題を解決することができますか?

from django.template import RequestContext 

@csrf_protect 
def register(request): 
    # ... 

    return render_to_response(
     'register.html', 
     {'form': form}, 
     RequestContext(request) 
    ) 

又はだけ1.8 docsからショートカット方法

@csrf_protect 
def register(request): 
    # ... 
    return render(request, 'register.html', {'form': form}) 

レンダリング使用:

+0

検査またはHTMLを送信できますか? –

+0

レンダリングされたHTMLは、htmlの登録を希望しますか? –

+0

はい。してください。私たちはそれを助けることができます –

答えて

5

コールrender_to_responseこのよう

(レンダリング)への呼び出しと同じですcontext_instance引数を持つrender_to_response() RequestContextの使用を強制します。

+0

Djangoはデフォルトでcsrf_tokenのポストリクエストを確認するので、csrf_protectを削除できます。 – MicroPyramid

+0

私はそれを試しましたが、私にはうまくいきません –

+0

'render'メソッドではなく' render_to_response'を使ってみましたか?私の答えのパート2? – mislavcimpersak