Я использую flash (), чтобы показать сообщение об ошибке для проверки в моем HTML-шаблон.Я получаю сообщение об ошибке:
"" "RuntimeError: сеанс недоступен, поскольку не был задан секретный ключ. Установите для secret_key в приложении что-то уникальное и секретное." ""
Я пытался создать секретный ключ сразу после app = Flask(__name__)
.Он работает, но не показывает flash () в шаблоне.Пожалуйста, помогите мне решить эту проблему.
Мой код Python:
from flask import Flask, render_template, url_for, request, redirect, flash
from flask_sqlalchemy import SQLAlchemy
#from send_email import send_email
from sqlalchemy import func
app = Flask(__name__)
app.secret_key = "some secrete key"
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:Password@localhost/TechEduMain'
db = SQLAlchemy(app)
class Student(db.Model):
id = db.Column(db.Integer, primary_key = True)
student_name = db.Column(db.String(30), nullable = False)
student_email = db.Column(db.String(30), unique = True, nullable = False)
student_password = db.Column(db.String(20), nullable = False)
def __repr__(self):
return f"Student('{self.student_name}', '{self.student_email}')"
class Client(db.Model):
id = db.Column(db.Integer, primary_key = True)
client_name = db.Column(db.String(30), nullable = False)
client_email = db.Column(db.String(30), unique = True, nullable = False)
client_password = db.Column(db.String(20), nullable = False)
def __repr__(self):
return f"Student('{self.client_name}', '{self.client_email}')"
posts = ['img_avatar2.png', 'img_avatar2.png', 'img_avatar2.png', 'img_avatar2.png']
@app.route('/')
def home():
return render_template("home.html", posts = posts)
@app.route('/home.html', methods = ['GET', 'POST'])
def on_submit():
if request.method == 'POST':
account = request.form['account']
email = request.form['email']
name = request.form['name']
password = request.form['password']
confirm_password = request.form['confirm_password']
if password == confirm_password:
if account == 'student':
student = Student(student_name = name, student_email = email, student_password = password)
db.session.add(student)
db.session.commit()
return render_template("home.html", posts = posts)
elif account == 'company':
company = Client(client_name = name, client_email = email, client_password = password)
db.session.add(company)
db.session.commit()
return render_template("home.html", posts = posts)
else:
flash("Password does not match. Please crosscheck and enter again!", "Danger")
return redirect(request.url)
return render_template("home.html", posts = posts)
@app.route('/signup.html', methods = ['GET', 'POST'])
def signup():
return render_template("signup.html")
@app.route('/login.html', methods = ['GET', 'POST'])
def login():
return render_template("login.html")
if __name__ == "__main__":
app.run(debug=True)
Мой HTML-шаблон:
{% extends "index.html" %}
{% block content %}
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600' rel='stylesheet' type='text/css'>
<link href="//netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.css" rel="stylesheet">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
<span>{{ message }}</span>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% endif %}
{% endwith %}
<div class="testbox">
<h1>Signup</h1>
<form action="/home.html" method="POST">
<hr>
<div class="accounttype">
<input type="radio" value="student" id="radioOne" name="account" checked/>
<label for="radioOne" class="radio" chec>Student</label>
<input type="radio" value="company" id="radioTwo" name="account" />
<label for="radioTwo" class="radio">Company</label>
</div>
<hr>
<label id="icon" for="name"><i class="icon-envelope "></i></label>
<input type="text" name="email" id="name" placeholder="Email" required/>
<label id="icon" for="name"><i class="icon-user"></i></label>
<input type="text" name="name" id="name" placeholder="Name" required/>
<label id="icon" for="name"><i class="icon-shield"></i></label>
<input type="password" name="password" id="name" placeholder="Password" required/>
<label id="icon" for="name"><i class="icon-shield"></i></label>
<input type="password" name="confirm_password" id="name" placeholder="Confirm Password" required/>
<p>Already a member? <a href="{{ url_for('login') }}">Login here!</a>.</p>
<input class="button" type="submit"/>
</form>
</div>
{% endblock content %}