繁体   English   中英

如何在 flask-admin Inherited Base 视图中允许 POST 方法?

[英]How to allow POST method in flask-admin Inherited Base view?

我已经扩展了 flask-admin 的 BaseView 以创建一个产品视图,我可以在其中将产品添加到我的数据库中。 但是在提交的时候。 它表明该方法是不允许的。

class ProductsView(BaseView):
    @expose('/')
    def add_products(self):
        form = AddProducts()
        if request.method == 'POST':
            print("Here")
            name = form.name.data
            price = form.price.data
            discount = form.price.data
            brand = form.brand.data
            description = form.description.data
            image = photos.save(request.files.get('image'))
            add_product = Products(
                name=name, price=price,
                discount=discount, brand=brand,
                desc=description,
                image_1=image
            )
            db.session.add(add_product)
            flash(f"{name} is added to the database successfully!!!", "success")
            db.session.commit()
            return redirect(url_for('home'))
        return self.render(template='products/addproducts.html', title="Add Products", form=form)

admin = Admin(app, name='Ekart Admin', template_mode='bootstrap3')
admin.add_view(ModelView(Products, db.session))
admin.add_view(ModelView(User, db.session))
admin.add_view(ProductsView(name='Add Products(Local)'))

在这里,我只需要在 add_product 视图中允许 post 方法。

你能试试吗? @expose('/', 方法=['POST'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM