2016-11-28 11 views
0

누락 : 단지 CSRF_Token없는 오류 메시지를 해결하기 위해 시도 이제 메신저를 들어플라스크 - CSRF_TOKEN 나는 다음과 같은 오류가 나타납니다 내 양식을 제출하려고 시도 할 때마다 오류

{'department': [u'Not a valid choice'], 'email': [u'This field is required.'], 'csrf_token': ['CSRF token missing'], 'name': [u'This field is required.'], 'address': [u'This field is required.']} 

합니다. 하지만 난

<form enctype="multipart/form-data" action="/index" method="post" role="form"> <!-- how the data is obtained from the form (POST method) --> 
    {{ form.csrf_token }} 
    <div class="form-group"> 
     <label style="margin-top: 10px;" for="name">Name:</label> 
     {{ form.name(class_="form-control") }} <!-- this creates the name form field --> 
     <br> 
     <label for="address">Address:</label> 
     {{ form.address(class_="form-control", rows='5', cols='40') }} <!-- this creates the adress form field --> 
     <br> 
     <label for="email">E-mail Address:</label> 
     {{ form.email(class_="form-control") }} 
     <br> 
     <label for="telephone">Phone Number: </label> 
     {{ form.telephone(class_="form-control") }} 
     <br> 
     <label for="file_upload">Upload CV: </label> 
     {{ form.file_upload(class_="form-control") }} 
     <br> 
     <label for="Department">Department:</label> 
     {{ form.department(class_="form-control")}} 
     <br> 
     </select> 
    </div> 
<button name="submit" type="submit" class="btn btn-primary">Submit</button> </form> <!-- submit button --> 

나는 또한 내 설정이 올바른지 생각 ...

WTF_CSRF_ENABLED = True 
SECRET_KEY = 'this-is-a-secret-key' 

내가 실종 ... 내 템플릿의 CSRF 토큰 태그 그래서 내가 왜 이런 일을 이해하고 있지 않다가 어떤 것? 어떤 도움을 주셔서 감사합니다!

편집 : (! 혼란 죄송합니다, 초보자) 여기의 요청에 따라 내 설정이다

from flask import Flask, render_template, session, flash, request, redirect,  url_for 
from flask_wtf import Form 
from flask_sqlalchemy import SQLAlchemy 
from werkzeug.utils import secure_filename 
from wtforms import TextField, TextAreaField, validators, StringField, SubmitField, BooleanField, RadioField, SelectField, FileField, IntegerField 
from .forms import ApplicationForm, DataRequired 
import os 
import re 
import sqlite3 
from flask_wtf.csrf import CsrfProtect 


SECRET_KEY = 'you-will-never-guess' 


#configuration 
DEBUG = True 
app = Flask('Application') 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///applicants.sqlite3' 

app.config.from_object(__name__) 
from app import views 

CsrfProtect(app) 
WTF_CSRF_ENABLED = True 

DEBUG = True 

UPLOAD_FOLDER = '/Uploads' 
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 
FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd']) 

def application(): 
    form = ApplicationForm(request.form) 
    return render_template('index.html','home.html', form=form) 

db = SQLAlchemy(app) 
class Applicants(db.Model): 
    id = db.Column('applicant_id', db.Integer, primary_key = True) 
    name = db.Column(db.String(100)) 
    address = db.Column(db.String(200)) 
    telephone = db.Column(db.String(15)) 
    email = db.Column(db.String(100)) 
    department = db.Column(db.String(30)) 
    file_upload = db.Column(db.Boolean) 

def __init__(self, name, address, telephone, email, department, file_upload): 
    self.name = name 
    self.address = address 
    self.telephone = telephone 
    self.email = email 
    self.department = department 
    self.file_upload = file_upload 

db.create_all() 

if __name__ == "Application": 
    app.run() 
+0

form.csrf를 통해'form.hidden_tag'을 사용해 보시겠습니까? http://stackoverflow.com/a/21501593 – Erik

+0

설정 개체를 표시 할 수 있습니까? 그것을 앱에 전달하는 방법은 무엇입니까? – ettanany

+0

@Erik는 hidden_tag와 csrf_token이 내가 믿는 것과 똑같은/비슷한 것을 할 때 다른 것을 시도하지 않았다. –

답변

0

나는 당신이 당신의 구성을 조금 정리 필요가 있다고 생각, 당신의 수입 아래 모든 것을 대체하려고 이와 def application(): 라인 위 :

app = Flask(__name__) 
CsrfProtect(app) 
app.config.update(
    DEBUG = True, 
    WTF_CSRF_ENABLED = True, 
    SECRET_KEY = 'you-will-never-guess', 
    UPLOAD_FOLDER = '/Uploads', 
    SQLALCHEMY_DATABASE_URI = 'sqlite:///applicants.sqlite3', 
    FILE_TYPES = ['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd'] 
) 
from app import views 

들이 제대로 CSRF 에러가 나오는 이유 인 설정되지 않았을 수 있습니다처럼 DEBUGSECRET_KEY 설정 모습입니다.

+0

이 픽스가 통합되었지만 여전히 문제가 발생하는 것 같습니다. 그러나 양식을 채우고 제출할 때 토큰이 누락됩니다. 양식을 비워두고 제출하면 오류가 발생하지 않습니다. 왜 이런 일이 일어나고 있는지 잘 모르겠지만 코드 작성을 도와 주셔서 감사합니다. –