#python #database #flask
Вопрос:
Я получаю эту ошибку сразу после того, как нажимаю кнопку «Отправить», которая находится на странице регистрации. Я создал файл .db, и класс пользователя объявлен на models.py файл с его свойствами.
Вот некоторые дополнительные сведения об ошибке: [SQL: ВСТАВЬТЕ В ЗНАЧЕНИЯ пользователя (адрес электронной почты, пароль, имя пользователя) (?, ?, ?)]
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3 Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
crossorigin="anonymous"
/>
<title>{% block title %}Home{% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar">
<div class="navbar-nav">
<a class="nav-item nav-link" id="login" href="/login">Login</a>
<a class="nav-item nav-link" id="signUp" href="/sign-up">Sign up</a>
<a class="nav-item nav-link" id="logout" href="/logout">Logout</a>
<a class="nav-item nav-link" id="home" href="/">Home</a>
</div>
</div>
</nav>
{% with messages = get_flashed_messages(with_categories=true) %} {% if
messages %} {% for category, message in messages %} {% if category ==
'error' %}
<div class="alert alert-danger alter-dismissable fade show" role="alert">
{{ message }}
<button type="button" class="close" data-dismiss="alert">
<span aria-hidden="true">amp;times;</span>
</button>
</div>
{% else %}
<div class="alert alert-success alter-dismissable fade show" role="alert">
{{ message }}
<button type="button" class="close" data-dismiss="alert">
<span aria-hidden="true">amp;times;</span>
</button>
</div>
{% endif %} {% endfor %} {% endif %} {% endwith %}
<div class="container">{% block content %} {% endblock %}</div>
<script
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"
></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5 76PVCmYl"
crossorigin="anonymous"
></script>
<script
type="text/javascript"
src="{{ url_for('static', filename='index.js') }}"
></script>
</body>
</html>
sign_up.html
{% extends "base.html" %} {% block title %}Sign Up{% endblock %} {% block
content %}
<form method="POST">
<h3 align="center">Sign Up</h3>
<div class="form-group">
<label for="email">Email Address</label>
<input
type="email"
class="form-control"
id="email"
name="email"
placeholder="Enter email"
/>
</div>
<div class="form-group">
<label for="firstName">First Name</label>
<input
type="text"
class="form-control"
id="firstName"
name="firstName"
placeholder="Enter first name"
/>
</div>
<div class="form-group">
<label for="password1">Password</label>
<input
type="password"
class="form-control"
id="password1"
name="password1"
placeholder="Enter password"
/>
</div>
<div class="form-group">
<label for="password2">Password (Confirm)</label>
<input
type="password"
class="form-control"
id="password2"
name="password2"
placeholder="Confirm password"
/>
</div>
<br />
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
__ init __.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
db = SQLAlchemy()
DB_NAME = "database.db"
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'asdfghhjkl'
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
db.init_app(app)
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
from .models import User, Note
create_database(app)
return app
def create_database(app):
if not path.exists('website/' DB_NAME):
db.create_all(app = app)
print('Created Database!')
auth.py
from typing import Text
from flask import Blueprint, render_template, request, flash, redirect, url_for
from werkzeug.wrappers import request as werkzeug_request
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
from . import db
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=['GET', 'POST'])
def login():
return render_template("login.html", boolean=True)
@auth.route('/logout')
def logout():
return "<p>logout</p>"
@auth.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
if request.method == 'POST':
email = request.form.get('email')
first_name = request.form.get('firstName')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
if len(email) < 4:
flash('Email must be greater than 4 characters', category='error')
elif len(first_name) < 2:
flash('First name must be greater than 2 characters', category='error')
elif password1 != password2:
flash('Passwords don't match', category='error')
elif len(password1) < 8:
flash('Password must contain at least 8 characters', category='error')
else:
new_user = User(email=email, first_name=first_name, password=generate_password_hash(password1, method='sha256'))
db.session.add(new_user)
db.session.commit()
flash('Account created!', category="success")
return redirect(url_for('views.home'))
return render_template("sign_up.html")
models.py
from datetime import timezone
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class Note(db.Model):
id = db.Column(db.Integer, primary_key = True)
data = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone = True), default = func.now())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key = True)
email = db.Column(db.String(150), unique = True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(150))
notes = db.relationship('Note')
views.py
from flask import Blueprint, render_template
views = Blueprint('views', __name__)
@views.route('/')
def home():
return render_template("home.html")
Ответ №1:
Если вы не импортировали models
до этого db.create_all()
, вам нечего будет создавать. Затем в импортированном коде все становится запутанным models
, так как этот код будет создавать запросы, предполагающие заполненную базу данных.