一、网站结构如下:
注意html页面需要放在名为templates的文件夹下,这是flask的规定。
二、Postgis操作
位于pgConnect.py中,代码如下:
#encoding: utf-8
import psycopg2
def insertGeo(start_time,end_time,reg_name,reg_radius,valid_time,reg_gov,guid,geom):
conn = psycopg2.connect(database="noFly",user="postgres",password="123",host="192.168.0.236",port="5433")
cur = conn.cursor()
sql="insert into tablename (is_enable,start_time,end_time,reg_name,reg_radius,valid_time,reg_gov,guid,geom) values ('1','{0}','{1}','{2}',{3},'{4}','{5}','{6}',ST_GeomFromGeoJson('{7}'))".format(start_time,end_time,reg_name,reg_radius,valid_time,reg_gov,guid,geom)
print(sql)
cur.execute(sql)
conn.commit()
print('更新成功')
三、模板页:
base.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- <link rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}"> -->
<style>
.nav{
background:rebeccapurple;
height:65px;
}
ul{
overflow: hidden;
}
ul li{
float: left;
list-style: none;
padding:0 10px;
line-height: 65px;
}
ul li a{
color: white;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li><a href="{{ url_for('update') }}">录入</a></li>
</ul>
</div>
{% block main %}
{% endblock %}
</body>
</html>
update.html代码:
{% extends "base.html" %}
{% block main %}
<form action="{{ url_for('update') }}" method="post">
<table>
<tbody>
<tr>
<td>开始时间:</td>
<td><input type="text" placeholder="2018-01-01" name="start_time" value="2018-01-01"/></td>
</tr>
<tr>
<td>结束时间:</td>
<td><input type="text" placeholder="2018-01-02" name="end_time" value="2018-01-02"/></td>
</tr>
<tr>
<td>区域名称:</td>
<td><input type="text" placeholder="aa" name="reg_name" value="aa"/></td>
</tr>
<tr>
<td>区域半径:</td>
<td><input type="text" placeholder="1" name="reg_radius" value="1"/></td>
</tr>
<tr>
<td>有效时间:</td>
<td><input type="text" placeholder="2018-01-01--2018-01-02" name="valid_time" value="2018-01-01--2018-01-02"/></td>
</tr>
<tr>
<td>发布单位:</td>
<td><input type="text" placeholder="国务院" name="reg_gov" value="国务院"/></td>
</tr>
<tr>
<td>唯一值:</td>
<td><input type="text" placeholder="eeee1111" name="guid" value="eeee1111"/></td>
</tr>
<tr>
<td>geojson:</td>
<td><input type="text" placeholder="geometry" name="geom" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="录入"/></td>
</tr>
</tbody>
</table>
</form>
{% endblock %}
启动文件,flaskTest.py代码:
# -*- coding: utf-8 -*-
# manman.py 一个简单的小程序
from flask import Flask,url_for,redirect,render_template,request
import pgConnect
import sys
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
@app.route('/')
def index():
return render_template('update.html')
@app.route('/api/update/',methods=['POST','GET'])
def update():
try:
# pgConnect.insertGeo(start_time,end_time,reg_name,reg_radius,valid_time,reg_gov,guid,geom)
# return "{'success':'true','mess':'更新成功'}"
if request.method == 'GET':
return render_template('update.html')
else:
start_time = request.form.get('start_time')#post请求。获取模版语言中输入框输入的值
end_time = request.form.get('end_time')
reg_name = request.form.get('reg_name')
reg_radius = request.form.get('reg_radius')
valid_time = request.form.get('valid_time')
reg_gov = request.form.get('reg_gov')
guid = request.form.get('guid')
geom = request.form.get('geom')
pgConnect.insertGeo(start_time,end_time,reg_name,reg_radius,valid_time,reg_gov,guid,geom)
return "{'success':'true','mess':'更新成功'}"
except Exception, e:
return "{'success':'false','mess':'更新失败:'"+str(e)+"}"
if __name__ == '__main__':
app.run(debug=True)#开启调试模式,这样就不必在修改代码之后手动重启了
完整代码地址:https://gitee.com/mingjingzhishui/python-flask/tree/postgis-insert/