D
D
domanskiy2020-09-11 12:38:51
JavaScript
domanskiy, 2020-09-11 12:38:51

How to make a dynamic HTML table and pass its values ​​through a post in Flask?

There are two related OTM database tables.
The bottom line is that tables with files are attached to the order number.
The tables (frames) themselves are recorded in one 'frames' database tab, where each row is linked to an order number by order id and to an order frame number through frame_num.
The matter is complicated by the fact that the number of lines in each frame is unlimited 1 + N, the number of frames themselves in the order is also not limited to 1 + N
It seems to be clearly described, but I can draw.

How to organize this in a template, with the setting (add a frame (table), add a line in the frame)?

models.py

class Frame(db.Model):
    """Модель таблицы файлов"""
    __tablename__ = 'frames'

    id = db.Column(db.Integer, primary_key=True)
    digital_orders_id = db.Column(db.Integer)
    frame_num = db.Column(db.SMALLINT)
    row_num = db.Column(db.SMALLINT)
    url_file = db.Column(db.String(255))

    def __init__(self, *args, **kwargs):
        super(Frame, self).__init__(*args, **kwargs)

    def __repr__(self):
        return f'< {self.id} {self.frame_num} от {self.digital_orders_id}>'


class DigitalOrder(db.Model):
    """Модель таблицы заказа"""
    __tablename__ = 'digital_orders'

    id = db.Column(db.Integer, primary_key=True)
    order = db.Column(db.String(140), nullable=False)
 
    frames_id = db.Column(db.Integer, db.ForeignKey('frames.id'), nullable=False)
    frames = db.relationship("Frame", backref=db.backref('digital_orders'))
    """id фреймов из таблицы frames"""

    def __repr__(self):
        return f'<{self.id} {self.order}>'


forms.py
from flask_wtf import Form
from wtforms.ext.sqlalchemy.fields import *

from wtforms import *
from models import *

class DigitalOrderForm(Form):
    order = StringField('Номер з-за')
    frames = IntegerField('Фреймы')


blueprint.py
from flask import Blueprint, session
from flask import render_template

from models import *
from .forms import DigitalOrderForm
from flask import request
from app import db
from flask import redirect
from flask import url_for
from models import slugify
from flask_security import login_required
digitalorders = Blueprint('digitalorders', __name__, template_folder='templates')

@digitalorders.route('/create', methods=['POST', 'GET'])
@login_required
def create_digitalorder():
    if request.method == 'POST':
        order = request.form['order']
        frames = request.form['frames']
        try:
            order = DigitalOrder(order=order, frames=frames)
            db.session.add(order)
            db.session.commit()
        except:
            print('Something wrong')

        return redirect(url_for('digitalorders.index.html'))

    form = DigitalOrderForm()
    return render_template('digitalorders/create.html', form=form)


Found the flask_table module. But it hasn't been updated in 5 years.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question