一组表单里面即包含文本框又有图片上传


一个小项目
表单包含2个文本域,一个上传图片的按钮 组成一条数据库记录

初学web开发, 边学flask边做 model,form表单写好了,数据库配置好了,
不知道怎样同时把带图片上传的表单一起组成一条数据提交

=======================.models.==========================

- - encoding:utf-8 - -

import datetime

author = 'afro'
from flask import Flask

from flask.ext.sqlalchemy import SQLAlchemy
from config import Config

app = Flask( name )
app.config['SQLALCHEMY_DATABASE_URI'] = Config.DB_URI
db = SQLAlchemy(app)

class Performer(db.Model):
tablename = 'audition'


 id = db.Column('act_id', db.Integer(20), primary_key=True, autoincrement=True, nullable=False)
act_name = db.Column('act_fullname', db.String(67), nullable=False)
act_tel = db.Column('act_num', db.String(14), unique=True, nullable=False)
uploaded_file = db.Column('act_photo', db.LargeBinary(16777216), nullable=False)
time_stamp = db.Column('timestamp', db.Date())

def __init__(self, act_name=None, act_tel=None, uploaded_file=None):
    self.act_tel = act_tel
    self.act_name = act_name
    self.uploaded_file = uploaded_file
    self.time_stamp = datetime.utcnow()

def __repr__(self):
    return '<Performer %s  %s>' % (self.act_name, self.act_tel)

if name == ' main ':
db.create_all()

=========================================================
<!DOCTYPE html>
<html>
<head lang="en">

<title>试镜申请表</title>
</head>
<body>

flashframe演员招募

候选人基本信息报名表

姓名 <input name="name" id="act_name" placeholder="杨羊羊" required="">

手机 <input type="tel" name="tel" id="act_tel" minlength="11" placeholder="(180) 5555 5555" required="">

选一张你最满意的照片:

<input type="file" name="uploaded_photo" id="photo">

<input type="submit" value="Sign up">

</body>
</html>

========================.forms.========================

- - encoding:utf-8 - -

author = 'afro'
from flask_wtf import Form
from wtforms import StringField, DateField
from wtforms.validators import DataRequired, NumberRange

class auditionform(Form):
act_name = StringField('StringField', validators=[DataRequired])
act_tel = StringField('StringField', validators=[
DataRequired], [NumberRange(min=11, max=13)])
time_stamp = StringField('DateField')
uploaded_file =

flask sqlalchemy blob wtforms

水中月xu 9 years, 9 months ago

Your Answer