1
    from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///books.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class horror(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    author = db.Column(db.String(50))
    content = db.Column(db.Text)
    price = db.Column(db.Integer)
    date_posted = db.Column(db.DateTime)

class teen(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    author = db.Column(db.String(50))
    content = db.Column(db.Text)
    price = db.Column(db.Integer)
    date_posted = db.Column(db.DateTime)

class adult(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    author = db.Column(db.String(50))
    content = db.Column(db.Text)
    price = db.Column(db.Integer)
    date_posted = db.Column(db.DateTime)

class fantasy(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    author = db.Column(db.String(50))
    content = db.Column(db.Text)
    price = db.Column(db.Integer)
    date_posted = db.Column(db.DateTime)

class bought(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    email = db.Column(db.String(50))
    phone = db.Column(db.String)
    city = db.Column(db.String)
    newpost = db.Column(db.String)
    promo = db.Column(db.String)


@app.route('/')
def index():
    return render_template('Главная.html')


@app.route('/')
def about():
    return render_template('Страница-1.html')


@app.route('/admin')
def addbook():
    return render_template('О-нас.html')

@app.route('/addpost', methods=['POST', 'GET'])
def addpost():
    genre = request.form['genre']
    title = request.form['title']
    price = request.form['price']
    author = request.form['author']
    content = request.form['content']
    if genre == 'horror':
        horror = horror(title=title, price=price, author=author, content=content)
        db.session.add(horror)
        db.session.commit()
        return redirect(url_for('Главная'))
    elif genre == 'fantasy':
        fantasy=fantasy(title=title, price=price, author=author, content=content)
        db.session.add(fantasy)
        db.session.commit()
    elif genre == 'adult':
        adult=adult(title=title, price=price, author=author, content=content)
        db.session.add(adult)
        db.session.commit()
    elif genre == 'teen':
        teen=teen(title=title, price=price, author=author, content=content)
        db.session.add(teen)
        db.session.commit()



@app.route('/')
def BYE():
    return render_template('Контакты.html')

if __name__ == '__main__':
    app.run(debug=True)

Ничего не могу понять. Должно работать, но не хочет. Подскажите что исправить. Спасибо,

1 ответ 1

1

Сделай классы с титульной буквой Horror

Далее вызывай конструктор класса:

horror = Horror(title=title, price=price, author=author, content=content)

Так же можно чуть пупростить твой код

if genre == 'horror':
    obj = horror(title=title, price=price, author=author, content=content)
elif genre == 'fantasy':
    obj=fantasy(title=title, price=price, author=author, content=content)
elif genre == 'adult':
    obj=adult(title=title, price=price, author=author, content=content)
elif genre == 'teen':
    obj=teen(title=title, price=price, author=author, content=content)
db.session.add(obj)
db.session.commit()
return redirect(url_for('Главная'))

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.