Keyerror: 'A secret key is required to use CSRF.'

Asked

Viewed 328 times

0

My application is showing error "Keyerror: 'A secret key is required to use CSRF.'", however in my config.py file I have the key created. What else could I check to remedy this error?

File config.py:

DEBUG = True

SECRET_KEY = 'minha_senha' 
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:''/meu_schema'
app.config['SECRET_KEY'] = SECRET_KEY

form py.

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.fields.html5 import EmailField
from wtforms.validators import DataRequired, Email

class form_cadastro(FlaskForm):
    nome = StringField('nome', validators=[DataRequired('Seu nome é requerido!')])
    sobrenome = StringField('sobrenome', validators=[DataRequired('Seu sobrenome é requerido!')])
    nickname = StringField('nickname', validators=[DataRequired('Seu Nick é requerido!')]) 
    email = EmailField('email', validators=[DataRequired('Seu e-mail é requerido!')])
    senha = PasswordField('password', validators=[DataRequired()])

HTML file cadastre.html

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Cadastro</title>
        <meta name="description" content="description">
        <meta name="author" content="Evgeniya">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="{{ url_for('static', filename='plugins/bootstrap/bootstrap.css') }}" rel="stylesheet">
        <link href="http://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css" rel="stylesheet">
        <link href='http://fonts.googleapis.com/css?family=Righteous' rel='stylesheet' type='text/css'>
        <link href="{{ url_for('static', filename='css/style_v2.css') }}" rel="stylesheet">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
                <script src="http://getbootstrap.com/docs-assets/js/html5shiv.js"></script>
                <script src="http://getbootstrap.com/docs-assets/js/respond.min.js"></script>
        <![endif]-->
    </head>
<body>
<div class="container-fluid">
    <div id="page-login" class="row">
        <div class="col-xs-12 col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3">
            <div class="text-right">
                <a href="/login" class="txt-default">Já tem uma conta?</a>
            </div>
            <div class="box">
                <div class="box-content">
                    <div class="text-center">
                        <h3 class="page-header">Cadastro NSP Dashboard Operacional</h3>
                    </div>
                    <form action"/cadastro" method="POST">
                        {{ form.csrf_token }}
                        <div class="form-group" action="" method="POST">
                            <label class="control-label">Nome</label>
                            {{ form.nome(_class="form-control", name="Nome", placeholder="Seu Nomes" )}}
                        </div>
                        <div class="form-group">
                            <label class="control-label">Sobrenome</label>
                            {{ form.sobrenome(_class="form-control" name="Sobrenome", placeholder="Seu Sobrenome")}}
                        </div>
                        <div class="form-group">
                            <label class="control-label">Nickname</label>
                            {{ form.nickname(_class="form-control" name="Nickname", placeholder="Seu Apelido")}}
                        </div>
                        <div class="form-group">
                            <label class="control-label">E-Mail</label>
                            {{ form.email(_class="form-control" name="email", placeholder="E-mail Corporativo")}}
                        </div>
                        <div class="form-group">
                            <label class="control-label">Password</label>
                            {{ form.password(_class="form-control" name="password")}}
                        </div>
                    </form>
                    <div class="text-center">
                        <a href="/" class="btn btn-primary" type='submit'>Cadastrar</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

1 answer

0

You need to put {{ form.csrf_token }} to include the token, he needs to stay inside the tags <FORM>...</FORM> of your form. By the way, both are missing from the example you published.

  • Good morning @Giovanni Nunes, thanks for the reply. I edited the html code above, I did not in this comment because it would not fit. However, it didn’t work, maybe because I’m doing something wrong yet, which would be normal because I don’t know about HTML. I took a model of how to use the tag in w3c, but I think it’s still wrong because it didn’t work.

  • In my file init.py added the following lines: &#xA;from flask import Flask&#xA;from flask_sqlalchemy import SQLAlchemy&#xA;from flask_wtf.csrf import CSRFProtect&#xA;&#xA;app = Flask(__name__)&#xA;csrf = CSRFProtect(app)&#xA;&#xA;db = SQLAlchemy(app)&#xA;&#xA;from app.controllers import default&#xA;&#xA;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.