flask-wtforms

Flask, WTForms: Is there a way to make a StringField in the form _temporarily_ hidden?

戏子无情 提交于 2019-12-23 23:17:51
问题 This is my model: class F(Form): a = StringField('a', validators = [validators.DataRequired()]) Is there a way to make a StringField in the form temporarily hidden? Something like: @app.route('/f', methods = ['GET', 'POST']) def f(): form = F(request.form) if foo(form): form.a.__MakeTemporarilyHidden__() else: form.a.__MakeItVisibleAgain__() if request.method == 'GET': return render_template('f.html', form = form) I am aware of wtforms.fields.HiddenField but I want to dynamically switch

Get current user id in Flask

巧了我就是萌 提交于 2019-12-23 16:26:40
问题 I'm quite new to Python (and, to be honest, programming in general). I'm currently working on a kind of to-do list, where I need it to put to-do items into appropriate course (it's all related to educational stuff). So, the problem is quite straight-forward. I have this as a Flask-driven route: @app.route('/add_course', methods=('GET', 'POST')) @login_required def course(): form = forms.CourseForm() if form.validate_on_submit(): models.Course.create(teacher=g.user._get_current_object(), name

Testing a POST that uses Flask-WTF validate_on_submit

ⅰ亾dé卋堺 提交于 2019-12-23 11:49:08
问题 I am stumped on testing a POST to add a category to the database where I've used Flask_WTF for validation and CSRF protection. For the CRUD operations pm my website. I've used Flask, Flask_WTF and Flask-SQLAlchemy. It is my first independent project, and I find myself a little at a lost on how to test the Flask-WTForm validate_on_submit function. Here's are the models: class Users(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) name = db.Column(db.String(80), nullable

Testing a POST that uses Flask-WTF validate_on_submit

﹥>﹥吖頭↗ 提交于 2019-12-23 11:49:07
问题 I am stumped on testing a POST to add a category to the database where I've used Flask_WTF for validation and CSRF protection. For the CRUD operations pm my website. I've used Flask, Flask_WTF and Flask-SQLAlchemy. It is my first independent project, and I find myself a little at a lost on how to test the Flask-WTForm validate_on_submit function. Here's are the models: class Users(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) name = db.Column(db.String(80), nullable

How to upload an image with flask and store in couchdb?

南楼画角 提交于 2019-12-23 05:48:04
问题 A previous question asks how to retrieve at attachment from couchdb and display it in a flask application. This question asks how to perform the opposite, i.e. how can an image be uploaded using flask and saved as a couchdb attachment. 回答1: Take a look at the example from WTF: from werkzeug.utils import secure_filename from flask_wtf.file import FileField class PhotoForm(FlaskForm): photo = FileField('Your photo') @app.route('/upload/', methods=('GET', 'POST')) def upload(): form = PhotoForm(

Setting a default on a select removes the settings passed in to populate the form

爱⌒轻易说出口 提交于 2019-12-22 18:16:18
问题 This code works fine separately. What I mean is when I set the default tag and call process() all the other data that should populate the form have been removed. In this case the default is ok, but the other fields are empty. form = ReviewForm(**populate_form) form.tags.default = '1' form.process() So, it seems like process cleans the **populate_form values out. I need to populate all fields and then set the default for the select. 回答1: From the WTForms Documentation: Since BaseForm does not

How to decode &#39 in flask with Jinja2 template [duplicate]

ε祈祈猫儿з 提交于 2019-12-21 14:21:24
问题 This question already has answers here : JavaScript raises SyntaxError with data rendered in Jinja template (2 answers) Closed 3 years ago . When I'm trying to write errors from wtforms in Jinja2 template, it returns undecoded quote. How can i fix it? {% if registrationForm.errors %} <script>swal("Error!", "{{ registrationForm.errors['password'] }}", "error")</script> {% endif %} Errors are equal to {'email': ['This field is required.'], 'username': ['This field is required.'], 'acceptTOS': [

How to use Flask-WTForms CSRF protection with AJAX?

≡放荡痞女 提交于 2019-12-20 13:31:23
问题 Flask-WTForms provides CSRF protection. It works great when using normal HTML forms, but the process is less clear when using AJAX. I have a file upload in my form, and I split the process in two with AJAX: the file goes to the upload endpoint while the rest of the form goes to the submit endpoint. Since the file is posted with AJAX, it doesn't get a CSRF token, but I want to protect the upload endpoint from attacks. How can I generate a CSRF token when using AJAX? @app.route('/submit',

Flask-WTF form has errors during GET request

匆匆过客 提交于 2019-12-20 04:08:21
问题 I have a Flask view with a Flask-WTF form. When I load the page in the browser, the form always has errors, even though I haven't submitted it yet. Why does the form have errors before it is submitted? @app.route('/', methods=['GET', 'POST']) def index(): form = ApplicationForm(request.form) if form.is_submitted(): print "Form successfully submitted" if form.validate(): print "valid" print(form.errors) if form.validate_on_submit(): return redirect('index') return render_template('index.html',

Flask-WTF CSRF validation fails when app moved to docker production environment

独自空忆成欢 提交于 2019-12-20 03:52:47
问题 I just set up my production environment for the Flask app I've been developing. This stack is: Windows Server 2012 R2 Hyper-V VM - Running Ubuntu 14.04 Docker 1.5 - Running Containers: mysql 5.6 nginx 1.6.3 uwsgi 2.0.10 So basically I have a static IP set up in Ubuntu, port 80 getting forwarded to the nginx container, a data volume shared between the nginx and uwsgi containers for a socket file, and finally a link between the mysql container and the uwsgi container for database communication.