我扩展了flask-admin的BaseView来创建一个产品 View ,我可以在其中将产品添加到我的数据库中。但是在提交的时候。显示该方法不被允许。
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 View 中允许 post 方法。
最佳答案
你能尝试一下吗? @expose('/',methods=['POST'])
关于python - 如何在flask-admin Inherited Base View 中允许POST方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66544393/