How to use HTML template for list columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.sqlalchemy import SQLAlchemy | |
from flask.ext import admin, wtf | |
from flask.ext.admin.model import template | |
from flask.ext.admin.contrib import sqlamodel | |
from flask.ext.admin.contrib.sqlamodel import filters | |
# Create application | |
app = Flask(__name__) | |
# Create dummy secrey key so we can use sessions | |
app.config['SECRET_KEY'] = '123456790' | |
# Create in-memory database | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite' | |
app.config['SQLALCHEMY_ECHO'] = True | |
db = SQLAlchemy(app) | |
# Create models | |
class Color(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String(32)) | |
value = db.Column(db.String(8)) | |
# Flask views | |
@app.route('/') | |
def index(): | |
return '<a href="/admin/">Click me to get to Admin!</a>' | |
# Color admin with custom template | |
class ColorAdmin(sqlamodel.ModelView): | |
# Force specific template | |
list_template = 'color_list.html' | |
create_template = 'color_form.html' | |
edit_template = 'color_form.html' | |
# Customize list view to call 'color' macro for value column | |
list_formatters = dict(value=template.macro('color')) | |
# Use select field for value column | |
form_overrides = dict(value=wtf.SelectField) | |
form_args = dict(value=dict( | |
choices=[ | |
('#000000', 'Black'), | |
('#ffffff', 'White'), | |
('#ff0000', 'Red'), | |
('#00ff00', 'Green'), | |
('#0000ff', 'Blue'), | |
])) | |
if __name__ == '__main__': | |
# Create admin | |
admin = admin.Admin(app, 'Simple Models') | |
# Add views | |
admin.add_view(ColorAdmin(Color, db.session)) | |
# Create DB | |
db.create_all() | |
# Start app | |
app.debug = True | |
app.run('0.0.0.0', 8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'admin/model/edit.html' %} | |
{% block head %} | |
{{ super() }} | |
<link href="{{ url_for('static', filename='simplecolorpicker.css') }}" rel="stylesheet"> | |
{% endblock %} | |
{% block tail %} | |
{{ super() }} | |
<script src="{{ url_for('static', filename='jquery.simplecolorpicker.js') }}"></script> | |
<script language="javascript"> | |
$('select#value').simplecolorpicker(); | |
</script> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'admin/model/list.html' %} | |
{% macro color(model, column) %} | |
<div style="width: 16px; height: 16px; border-radius: 5px; background-color: {{ model.value }}"> </div> | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment