如何使用sqlalchemy和flask更新表

问题描述 投票:0回答:1

有人可以在这里帮我吗?我正在尝试使用Flask完成CRUD,并且在尝试在数据库上编辑客户端时遇到一些问题。我没有错误,但是没有更新。我不确定它是否有些路由问题,我不清楚。我正在发布我认为可能会有所帮助的文件,但请告诉我是否还有其他需要。谢谢和问候!

app.py

login_manager = LoginManager(app)
login_manager.login_view = "login"
db = SQLAlchemy(app)

from models import User, Client

@app.route("/")
def index():
    user_id = current_user.get_id()
    clients = Client.get_by_user_id(user_id)
    return render_template("index.html", clients=clients)


@app.route("/client/<int:id>/", methods=['GET', 'POST'])
def show_client(id):

    client = Client.get_by_id(id)
    form = ClientEditForm(obj=client)
    if client is None:
        abort(404)
    return render_template("client_view.html", client=client, form=form)

@app.route("/admin/client/", methods=['GET', 'POST'], defaults={'client_id': None})
@app.route("/admin/client/<int:client_id>/", methods=['GET', 'POST'])
@login_required
def client_form(client_id):
    form = ClientForm()
    if form.validate_on_submit():
        name = form.name.data
        last_name = form.last_name.data
        cuit = form.cuit.data
        password = form.password.data
        date_from = form.date_from.data
        date_to = form.date_to.data
        category = form.category.data

        client = Client(user_id=current_user.id, name=name, last_name=last_name, cuit=cuit, password=password ,date_from=date_from, date_to=date_to, category=category)
        client.save()

        return redirect(url_for('index'))
    return render_template("admin/client_form.html", form=form)

#start client edit

@app.route("/clientEdit/", methods=['GET', 'POST'], defaults={'client_id': None})
@app.route("/clientEdit/<int:client_id>/", methods=['GET', 'POST'])
@login_required
def client_edit_form(client_id):
    form = ClientEditForm()
    if form.validate_on_submit():
        name = form.name.data
        last_name = form.last_name.data
        cuit = form.cuit.data
        password = form.password.data
        date_from = form.date_from.data
        date_to = form.date_to.data
        category = form.category.data
        client = Client(user_id=current_user.id, name=name, last_name=last_name, cuit=cuit, password=password ,date_from=date_from, date_to=date_to, category=category)
        client.save()


        return redirect(url_for('index'))
    return render_template("client_view.html", client=client, form=form)

#end client edit

@app.route("/signup/", methods=["GET", "POST"])
def show_signup_form():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = SignupForm()
    error = None
    if form.validate_on_submit():
        name = form.name.data
        email = form.email.data
        password = form.password.data
        user = User.get_by_email(email)
        if user is not None:
            error = f'El email {email} ya está siendo utilizado por otro usuario'
        else:
            user = User(name=name, email=email)
            user.set_password(password)
            user.save()
            login_user(user, remember=True)
            next_page = request.args.get('next', None)
            if not next_page or url_parse(next_page).netloc != '':
                next_page = url_for('index')
            return redirect(next_page)
    return render_template("signup_form.html", form=form, error=error)

@login_manager.user_loader
def load_user(user_id):
    return User.get_by_id(int(user_id))

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.get_by_email(form.email.data)
        if user is not None and user.check_password(form.password.data):
            login_user(user, remember=form.remember_me.data)
            next_page = request.args.get('next')
            if not next_page or url_parse(next_page).netloc != '':
                next_page = url_for('index')
            return redirect(next_page)
    return render_template('login_form.html', form=form)

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

client_view.html

{% extends "base_template.html" %}

{% block title %}{{ client.cuit }}{% endblock %}

{% block content %}
<h1>{{ client.last_name }}</h1>
{{ client.name }}
{{ client.cuit }}

<form action="" method="post" novalidate>
    {{ form.hidden_tag() }}
    <div>
        {{ form.name.label }}
        {{ form.name(size=60) }}<br>
        {% for error in form.name.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.last_name.label }}
        {{ form.last_name(size=60) }}<br>
        {% for error in form.last_name.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.cuit.label }}
        {{ form.cuit }}<br>
        {% for error in form.cuit.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.password.label }}
        {{ form.password }}<br>
        {% for error in form.password.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.date_from.label }}
        {{ form.date_from }}<br>
        {% for error in form.date_from.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.date_to.label }}
        {{ form.date_to }}<br>
        {% for error in form.date_to.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.category.label }}
        {{ form.category }}<br>
        {% for error in form.category.errors %}
        <span style="color: red;">{{ error }}</span>
        {% endfor %}
    </div>
    <div>
        {{ form.submit() }}
    </div>
</form>
{% endblock %}

forms.py

from flask_wtf import FlaskForm, RecaptchaField
from wtforms import StringField, SubmitField, PasswordField, TextAreaField, BooleanField
from wtforms.validators import DataRequired, Email, Length


class SignupForm(FlaskForm):
    name = StringField('Nombre', validators=[DataRequired(), Length(max=64)])
    password = PasswordField('Password', validators=[DataRequired()])
    #email = StringField('Email', validators=[DataRequired(), Email()])
    email = StringField('Email', [Email(message=('Not a valid email address!!!.')), DataRequired()])
    #recaptcha = RecaptchaField()
    submit = SubmitField('Registrar')

class ClientForm(FlaskForm):
    name = StringField('Nombre', validators=[DataRequired(), Length(max=25)])
    last_name = StringField('Apellido', validators=[DataRequired(),Length(max=25)])
    cuit = StringField('Cuit', validators=[DataRequired(),Length(max=11)])
    password = PasswordField('Password', validators=[DataRequired()])
    date_from = StringField('Fecha desde', validators=[DataRequired(),Length(max=10)])
    date_to = StringField('Fecha hasta', validators=[DataRequired(),Length(max=10)])
    category = StringField('Categoria', validators=[DataRequired()])
    submit = SubmitField('Enviar')

class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember_me = BooleanField('Recuérdame')
    submit = SubmitField('Login')

class ClientEditForm(FlaskForm):
    name = StringField('Nombre', validators=[DataRequired(), Length(max=25)])
    last_name = StringField('Apellido', validators=[DataRequired(),Length(max=25)])
    cuit = StringField('Cuit', validators=[DataRequired(),Length(max=11)])
    password = PasswordField('Password', validators=[DataRequired()])
    date_from = StringField('Fecha desde', validators=[DataRequired(),Length(max=10)])
    date_to = StringField('Fecha hasta', validators=[DataRequired(),Length(max=10)])
    category = StringField('Categoria', validators=[DataRequired()])
    submit = SubmitField('Enviar')

models.py

从烧瓶导入URL_for

from flask_login import UserMixin
from slugify import slugify
from sqlalchemy.exc import IntegrityError
from werkzeug.security import generate_password_hash, check_password_hash
from app import db


class User(db.Model, UserMixin):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    email = db.Column(db.String(256), unique=True, nullable=False)
    password = db.Column(db.String(128), nullable=False)
    is_admin = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return f'<User {self.email}>'

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, password)

    def save(self):
        if not self.id:
            db.session.add(self)
        db.session.commit()

    @staticmethod
    def get_by_id(id):
        return User.query.get(id)

    @staticmethod
    def get_by_email(email):
        return User.query.filter_by(email=email).first()

class Client(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
    name = db.Column(db.String(25), nullable=False)
    last_name = db.Column(db.String(25), nullable=False)
    cuit = db.Column(db.String(11), nullable=False)
    password = db.Column(db.String(128), nullable=False)
    date_from = db.Column(db.String(10), nullable=False)
    date_to = db.Column(db.String(10), nullable=False)
    category = db.Column(db.String(1), nullable=True)

    def __repr__(self):
        return f'<Post {self.last_name}>'
    def save(self):
        if not self.id:
            db.session.add(self)
        saved = False
        count = 0
        while not saved:
            try:
                print("tero")
                db.session.commit()
                saved = True
            except IntegrityError:
                count += 1

    def public_url(self):
        return url_for('show_client', id=self.id)


    @staticmethod
    def get_by_id(id):
        return Client.query.get(id)
    @staticmethod
    def get_all():
        return Client.query.all()
    @staticmethod
    def get_by_user_id(user_id):
        return Client.query.filter_by(user_id=user_id).all()
python flask sqlalchemy wtforms
1个回答
0
投票

您需要将添加/编辑的对象添加到db会话中,如果希望将所做的更改保存在数据库中,则提交。

我不知道您的client.save()中有什么,但是在此阶段您应该执行db.session.add(client)db.session.commit()

refer to Flask-SQLalchemy doc

© www.soinside.com 2019 - 2024. All rights reserved.