如何在 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('/', medthods=['POST'])
我扩展了 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('/', medthods=['POST'])