使用 flask-admin 和 sqlalchemy;现在我正在尝试为 add/delete/edit 我的 table 创建一个表格

using flask-admin and sqlalchemy; now i am trying to make a form to add/delete/edit my table

我创建了一个自定义视图,以类似手风琴的方式组织我的所有产品和服务,以便数据对用户友好。下面是我的自定义视图:

class productView(BaseView):
    form_columns = ['name','price']
    @expose('/')
    def index(self):
        myServ = Product.query.all()
        myProd =  Productcategories.query.all()
        return self.render('admin/products.html', myProd = myProd, myServ = myServ)


admin.add_view(productView(name='Products and Services', endpoint='products'))

然后我开始制作我的 HTML 视图,效果非常好。我现在所处的位置是我创建了一些按钮,这些按钮使用我的 jquery 来加载模式。这也很好用。但是,我有点卡在了下一步中。

一旦我点击 "submit" 按钮,我如何告诉它 add/delete/edit 特定的项目?

//button scripts
$(document).ready(function() {
  $(".add-category-item").click(function() {
    $('.bg-modal').fadeIn('slow');
  });

  $(".close").click(function() {
    $('.bg-modal').fadeOut('slow');
  });
});

$(document).ready(function() {
  $(".subtract-category-item").click(function() {
    $('.bg-modal').fadeIn('slow');
  });

  $(".close").click(function() {
    $('.bg-modal').fadeOut('slow');
  });
});

$(document).ready(function() {
  $(".edit-category-item").click(function() {
    var parent = $(this).attr("data-parent");
    $('#papa').val(parent);
    console.log("the parent caregory is:" + parent);
    $('.bg-modal').fadeIn('slow');
  });

  $(".close").click(function() {
    $('.bg-modal').fadeOut('slow');
  });
});
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<li> {{prod.category_name}} <a class="edit-category-item" data-parent="{{prod.parent_category_id}}" href="#"><span class="glyphicon glyphicon-pencil"></span></a> <a class="subtract-category-item" href="#"><span class="glyphicon glyphicon-minus"></span></a>  <a class="add-category-item" href="#"><span class="glyphicon glyphicon-plus"></span></a></li>


<a class="edit-service-item" href="#"><span class="glyphicon glyphicon-pencil"></span></a> <a class="subtract-service-item" href="#"><span class="glyphicon glyphicon-minus"></span></a> <a class="add-service-item" href="#"><span class="glyphicon glyphicon-plus"></span></a>


<!-- Modal -->
<div style="display: none;" class="bg-modal">
  <div class="modal-contents">

    <div class="close">+</div>

    <form action="">
      <input type="text" placeholder="Name of category">
      <input type="text" id="papa">
      <a href="#" class="button">Submit</a>
    </form>

  </div>
</div>

如果您不使用 flask-admin 通用删除和编辑视图,您可以公开一个路由来为您执行 delete/edit 并将此路由添加为您的表单 action

例如

class productView(BaseView):
    form_columns = ['name','price']
    @expose('/')
    def index(self):
        myServ = Product.query.all()
        myProd =  Productcategories.query.all()
        return self.render('admin/products.html', myProd = myProd, myServ = myServ)

    @expose('/custom_delete')
    def custom_delete(self):
        # write custom code to delete from the database
        return self.render('admin/products.html', myProd = myProd, myServ = myServ)

然后在你的 HTML

<div style="display: none;" class="bg-modal">
  <div class="modal-contents">

    <div class="close">+</div>

    <form action="{{url_for('.custom_delete')}}">
      <input type="text" placeholder="Name of category">
      <input type="text" id="papa">
      <a href="#" class="button">Submit</a>
    </form>

  </div>
</div>

您的表单将被提交到端点,您可以在端点处理后端的删除操作