flask-admin 以正确的方式处理请求

flask-admin handle request the right way

如何处理来自列表视图模板的请求。如果我单击 custom_list.html 中的提交按钮,变量不会输出为字符串。我做错了什么?

app.py

from flask import Flask
import flask_admin as admin
from flask_mongoengine import MongoEngine
from flask_admin.contrib.mongoengine import ModelView
from flask_admin.actions import action

# Create application
app = Flask(__name__)

# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
app.config['MONGODB_SETTINGS'] = {'DB': 'test_app'}

# Create models
db = MongoEngine()
db.init_app(app)


class Website(db.Document):
    domain_name = db.StringField(max_length=200)
    title = db.StringField(max_length=160)
    meta_desc = db.StringField()


class WebsiteView(ModelView):

    list_template = 'custom_list.html'

    @action('create_meta', 'Create Meta', 'Are you sure you want to create meta data?')
    def action_createmeta(self, ids):

        print "this is my domain_name {} and this is my title {}".format(
            Website.domain_name, Website.title)


# Flask views
@app.route('/')
def index():
    return '<a href="/admin/">Click me to get to Admin!</a>'


if __name__ == '__main__':
    # Create admin
    admin = admin.Admin(app, 'Example: MongoEngine')

    # Add views
    admin.add_view(WebsiteView(Website))

    # Start app
    app.run(debug=True)

templates/custom_list.html

{% extends 'admin/model/list.html' %} 
{% block body %} 
    <h1>Custom List View</h1> 
    {{ super() }} 
{% endblock %}

{% block list_row_actions %} 
    {{ super() }} 

  <form class="icon" method="POST" action="/admin/website/action/">
    <input id="action" name="action" value="create_meta" type="hidden">
    <input name="rowid" value="{{ get_pk_value(row) }}" type="hidden">
    <button onclick="return confirm('Are you sure you want to create meta data?');" title="Create Meta">
      <span class="fa fa-ok icon-ok"></span>
    </button>
  </form>
{% endblock %}

产出

您打印的是 Website.domain_name 的定义,而不是值。

documentation and example你需要做的:

for id in ids:
    found_object = Website.objects.get(id=id)
    print "this is my domain_name {} and this is my title {}".format(
          found_object.domain_name, found_object.title)

编辑:原始 post。

Change the line

print "this is my domain_name {} and this is my title {}".format(
        Website.domain_name, Website.title)

to

print "this is my domain_name {} and this is my title {}".format(
        Website.domain_name.data, Website.title.data)

You are printing the objects not the posted data contained within them