我試圖讓文件上傳flask-uploads
工作,並在一些斷枝運行。我會告訴你我的flask
查看功能,HTML和希望有人可以指出我錯過了什麼。WTforms形式不提交,但不輸出驗證錯誤
基本上什麼情況是,我提交表單,也未能在視圖功能if request.method == 'POST' and form.validate():
檢查。它跳下來顯示模板。 wtforms沒有踢我表單上的任何錯誤,所以我想知道爲什麼它的失敗,如果聲明。
我在看什麼?
設置燒瓶上傳:
# Flask-Uploads
photos = UploadSet('photos', IMAGES)
configure_uploads(app, (photos))
查看:
def backend_uploadphoto():
from Application import photos
from Application.forms.backend import AddPhotoForm
clients = Client.query.all()
events = Event.query.order_by('date').all()
form = AddPhotoForm(request.form, csrf_enabled=True)
if request.method == 'POST' and form.validate():
from uuid import uuid4
uuid = uuid4()
filename = '{0}.jpg'.format(uuid)
photo = Photo(uid=uuid, client=request.form['client'], event=request.form['event'])
photofile = photos.save(request.files.get('photo'), photo.filename)
return redirect(url_for('backend'))
return render_template('backend/addphoto.html', form=form, clients=clients, events=events)
形式:
class AddPhotoForm(Form):
photo = FileField('Photo')
client = IntegerField('Client:')
event = IntegerField('Event:')
HTML:
<form action="{{url_for('backend_uploadphoto')}}" method="post">
<p>
{{form.client.label}}
<select name="client">
{% for client in clients %}
<option value="{{client.id}}">{{client.fullname}}</option>
{% endfor %}
</select>
{{form.client.errors}}
</p>
<p>
{{form.event.label}}
<select name="event">
{% for event in events %}
<option value="{{event.id}}">{{event.name}}</option>
{% endfor %}
</select>
{{form.event.errors}}
</p>
<p><label for="photo">Photo:</label>{{form.photo}} <input type="submit" value="Upload"> {{form.photo.errors}}</p>
</form>
那麼,如果你使用瓶,我會說 - 嘗試瓶,跆拳道。 –
@IgnasButėnas整個星期都收到了類似的錯誤,但它絕對不是CSRF相關,不幸=( – andrewpederson