Answer the question
In order to leave comments, you need to log in
What is wrong with sqlalchemy?
In general, I am creating a site on a flask with registration, most likely I misunderstood something and didn’t design it at all. I have the following error:
The code is like this:
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///user.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer(), primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
email = db.Column(db.String(100), nullable=False, unique=True)
password_hash = db.Column(db.String(100), nullable=False)
created_on = db.Column(db.DateTime(), default=datetime.utcnow)
updated_on = db.Column(db.DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow)
def __repr__(self):
return '<User %r>' % self.id
@app.route('/', methods=['POST', 'GET'])
def main_page():
return render_template('index.html')
@app.route('/services', methods=['POST', 'GET'])
def services_page():
return render_template('services.html')
@app.route('/contacts', methods=['POST', 'GET'])
def contacts_page():
return render_template('contacts.html')
@app.route('/signup', methods=['POST', 'GET'])
def signup_page():
if request.method == 'POST':
email = request.form['email']
username = request.form['username']
password_hash = request.form['password_hash']
user = User(email = email, username = username, password_hash = password_hash)
db.session.add(user)
db.session.commit()
else:
return render_template('signup.html')
@app.route('/login', methods=['POST', 'GET'])
def login_page():
return render_template('login.html')
if __name__ == '__main__':
app.run(debug=True)
{% extends 'base.html' %}
{% block title %}
Регистрация
{% endblock %}
{% block body %}
<div class="text-center">
<div class="row h-100 justify-content-center align-items-center">
<form class="form-signin" method="post">
<h1 class="h3 mb-3 font-weight-normal">Пожалуйста, зарегистрируйтесь</h1>
<label for="inputEmail" class="sr-only">Адрес электронной почты</label>
<input name="email" id="email" type="email" id="inputEmail" class="form-control" placeholder="Адрес электронной почты" required="" autofocus="">
<label for="inputNickname" class="sr-only">Логин</label>
<input name="username" id="username" type="nickname" id="inputNickname" class="form-control" placeholder="Логин" required="" autofocus="">
<label for="inputPassword" class="sr-only">Придумайте пароль понадежнее</label>
<input name="password_hash" id="password_hash" type="password" id="inputPassword" class="form-control" placeholder="Придумайте пароль понадежнее" required="">
<div class="checkbox mb-3">
</div>
<input type="submit" class="btn btn-lg btn-block btn-primary" value="Зарегистрироваться">
<a href="/login" class="btn btn-lg btn-block btn-secondary btn-wide transition-3d-hover">У меня уже есть аккаунт</a>
</form>
</div>
</div>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Have you initialized the database?
To create the initial database, just import the db object from an interactive Python shell and run the SQLAlchemy.create_all() method to create the tables and database:
>>> from yourapplication import db >>> db.create_all()
Boom, and there is your database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question