Reputation: 784
how can i handle the request from listview template. if i click the submit button in my custom_list.html the the variables were not output as string. what did i do wrong ?
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 %}
output
Upvotes: 2
Views: 1069
Reputation: 621
You are printing the definitions of Website.domain_name not the values.
From the documentation and example you need to do:
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)
EDIT: original 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
Upvotes: 1