flask admin是flask框架中一个非常好用的后台管理框架,但是由于文档内容太少,经常遇到问题无法解决,这里记录一下

一简单的使用

from flask import Flask
from flask_admin import Admin
# csrf
from flask_admin.form import SecureForm
from flask_babelex import Babel
# 文件上传
# from flask_admin import form as admin_form from wtforms import form, fields, validators from flask_admin.contrib.pymongo import ModelView
from pymongo import MongoClient from werkzeug.security import check_password_hash # 文件上传
# from flask_admin.contrib.fileadmin import FileAdmin
# import os.path as op # file_path = op.join(op.dirname(__file__), 'static') app = Flask(__name__)
babel = Babel(app)
app.config['SECRET_KEY'] = 'xxx'
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN'
client = MongoClient(host='xxx’, port=27017)
db = client.test
db.authenticate('xxx', 'xxx')
Rentals = db.rentals class RentalsForm(form.Form):
city = fields.StringField(u'城市') class RentalsView(ModelView):
column_labels = dict(
_id = u'编号',
city = u'城市',
) form_base_class = SecureForm
# 文件上传
# form_extra_fields = {
# 'picture': admin_form.ImageUploadField(label=u'照片', base_path=file_path)
# }
column_list = ['_id', 'city']
form = RentalsForm if __name__ == '__main__':
admin = Admin(app, name=u'后台管理系统')
admin.add_view(RentalsView(Rentals, name=u'个人房源'))
app.run

二filed.FieldList,field.FormField的使用

在使用数据库mongodb的时候会遇到某个字段是个列表

这时候就可以使用field.FieldList

features = fields.FieldList(unbound_field=fields.StringField(u'配置'), label=u'房屋配置', min_entries=10)

这样编辑的时候就会有10个配置可以编辑,修改min_entries可以增加编辑的个数

保存之后是个列表

但是如果该字段是个列表,列表元素都是字典怎么办

这时候可以搭配FormField来使用

class PictureForm(form.Form):
id = fields.StringField(u'编号')
description = fields.StringField(u'描述')
url = fields.StringField(u'url路径')
    picture = fields.FieldList(unbound_field=fields.FormField(PictureForm), label=u'图片', min_entries=10)

搞定了

flask admin学习记录的更多相关文章

  1. Phyton Flask框架学习记录。

    注意:在左侧菜单栏(在JQuery插件库下载的)右边是采用<iframe> 标签嵌入其他页面,此时标签的src应用用后台中的方法名称(本人测试用的是无参数的方法), 而页面跳转window ...

  2. [ZHUAN]Flask学习记录之Flask-SQLAlchemy

    From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...

  3. Apache Shiro 学习记录4

    今天看了教程的第三章...是关于授权的......和以前一样.....自己也研究了下....我觉得看那篇教程怎么说呢.....总体上是为数不多的精品教程了吧....但是有些地方确实是讲的太少了.... ...

  4. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  5. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  6. mondb 常用命令学习记录

    mondb 常用命令学习记录 一.MongoDB 下载安装 MongoDB官网 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制 ...

  7. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  8. SVN教程 -- 基于自己学习记录

    SVN教程 -- 基于自己学习记录 1. 概述 a. 什么是SVN? Apache Subversion 通常被缩写成 SVN,是一个开放源代码的版本控制系统.相较于 git ,svn 是集中式版本控 ...

  9. Solr学习记录:Getting started

    目录 Solr学习记录:Getting started 1.Solr Tutorial 2. A Quick Overview Solr学习记录:Getting started 本教程使用环境:jav ...

随机推荐

  1. LeetCode 5:Given an input string, reverse the string word by word.

    problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...

  2. HDU1083(二分图最大匹配vector实现)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. 基于vlc sdk的二次开发--环境搭建、编译

    前言 关于 搭建.编译VLC,不同的平台有不同的方法,可以参考wiki. 其中在windows下编译VLC有两种方式,MSYS+MinGW和CygWin.通过测试,最后决定采用MSYS+MinGW搭建 ...

  4. echarts 图表建立步骤

    需要引用的文件 <script src="web/mobile/js/jquery-1.8.3.min.js"></script> <script s ...

  5. JavaSript中数组方法是否对原数组产生影响

    JavaScript中数组方法有很多.某次面试被问到,concat()方法会对影响到原数组吗.当时记得不牢,犹豫地说"会吧...".于是决定总结一下哪些数组方法会对原数组产生影响. ...

  6. quote(),unquote(),urlencode()编码解码

    quote(),unquote(),quote_plus(),unquote_plus(),urlencode() ,pathname2url(),url2pathname() urllib中还提供了 ...

  7. js一段小代码(浏览器用alert,否则用console)

    (function(){ var root=this, isBrowserSide=false; if(typeof window !=="undefined" && ...

  8. rtsp 学习

    1.1.   RTSP协议简介 一种应用层协议,可基于tcp或udp协议. Real Time Streaming Protocol或者RTSP(实时流媒体协议),是由Real network 和 N ...

  9. Tornado 模块概述

    Tornado模块分类 1. Core web framework tornado.web — 包含web框架的大部分主要功能,包含RequestHandler和Application两个重要的类 t ...

  10. CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】

    You are given an array d1,d2,-,dn consisting of n integer numbers. Your task is to split this array ...