Flask

Apply Flask MethodView decorator just for some methods

南笙酒味 提交于 2021-01-28 07:12:01
问题 I was wondering if there is any way that I can use a decorator just for some methods from my class, for example in the following code I want token_required to decorate all methods excepting the POST, how could I possibly achieve that? class UserAPI(MethodView): def token_required(view_method): @wraps(view_method) def decorated(*args, **kwargs): token = None if 'token' in request.headers: token = request.headers['token'] if not token: return "no token" return view_method(*args, **kwargs)

Python Flask Hosting on Windows 10 IIS server

末鹿安然 提交于 2021-01-28 06:11:42
问题 I Want to Host my Python Rest API on Windows 10 IIS server. First I tried to host a sample application but can not able to that. my_app.py from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello IIS from Flask framework.' @app.route('/Hello') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run() web.config <configuration> <system.webServer> <handlers> <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor=

Edit incoming request body payloads in flask api

与世无争的帅哥 提交于 2021-01-28 05:52:04
问题 Im looking to make my Flask based API case insensitive for all incoming payloads. rather than having to apply this to all api-route functions i want to apply this to the @app.before_request decorator, such that for all incoming requests with json payloads (POSTs and PUTs) I directly edit the payload before it is handled by the applicable app.route function. POST {"x":1, "Y":2} Should be formatted to POST {"x":1, "y":2} for request endpoints, but I can't seem to make this happen. @app.before

How to redirect from a JSON response?

谁说我不能喝 提交于 2021-01-28 05:51:47
问题 So I am trying to use Flask and a Javascript uploader(Dropzone) to upload files and redirect after the upload is complete. Files are getting uploaded fine, but using the traditional redirect in flask: return (redirect ("http://somesite.com")) Does nothing, page does not change. I think this is because of this: The request headers of the files being sent are set to Accept:"application/json" , and the response headers are being sent in <"text/html; charset=utf-8" How can I return a json

How to keep input after failed form validation in flask

若如初见. 提交于 2021-01-28 05:21:33
问题 With django I could simply pass the POST data to the form after failed validation and the user would not have to enter everything again: question_form = QuestionForm(request.POST) choice_form_set = ChoiceFormSet(request.POST) How could I implement this on my own in flask? 回答1: It's pretty similarly possible with Flask as well: @app.route('/register', methods=['GET', 'POST']) def register(): form = RegistrationForm(request.form) if request.method == 'POST' and form.validate(): user = User(form

Python authlib flask - how to revoke token / logout

最后都变了- 提交于 2021-01-28 05:14:46
问题 In my current situation my flask app could be the only one using the Oauth server. In that case I'd like to have my logout button actually revoke the access token. But how to do that using the authlib flask integration? Do I have to set up a new OAuth2Session just to use revoke_token() ? I don't think there is a hidden one inside FlaskRemoteApp ? And I don't think the flask registry takes a revoke_token_url or anything like that? Any advice is welcome. 回答1: Ok, here is the code that works for

FileAllowed does not display an error message

余生长醉 提交于 2021-01-28 05:09:09
问题 I am using WTForms. I am applying validation on file upload and restricted it to jpg,png and pdf format only. however if i give an incorrect input, no error message appears. I followed this tutorial https://flask-wtf.readthedocs.io/en/stable/form.html photo = FileField('photo', validators=[ FileRequired(), FileAllowed(['png', 'pdf', 'jpg'], "wrong format!") ]) 回答1: By default, flask-wtf does not show any error message if validation fails. Error messages can be caught and shown for each

Convert a bound method in python to a function (and reduce arg count)

烈酒焚心 提交于 2021-01-28 05:06:48
问题 I am adding a URL handler to a Flask application using add_url_rule. It requires a function as a handler (not a method). However, for proper encapsulation, my intended function is a bound method that uses the self variable. Is there any way I can somehow make Flask see my function: def action(self, param) as def action(param) ? class A(): def __init__(self): self._localvar = 5 self._flask = Flask("testapp") def add_rules(self): self._flask.add_url_rule("/","root",????) def action(self, value)

How to install Flask-mysqldb for Python?

蓝咒 提交于 2021-01-28 04:42:43
问题 I am new to Python and Python-Flask and have ran into an error. I am using Ubuntu 18.04.3 LTS and Python 2.7.15+ to build a python-flask web application. When installing flask-mysqldb I run into an error. $ pip install flask-mysqldb The full output: DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2

Flask-Restful - Return json with nested array

廉价感情. 提交于 2021-01-28 04:08:05
问题 My model consists in users and books. I want to nest all the books as an array of objects inside each user object, in order to show the books that each user owns. I'm trying to use marshal to nest the books as a list inside the users fields but nothing happens. In the response, there is only the array of users but no track of books and neither of an error. The idea is this: books_fields = { 'id': fields.Integer, 'type' : fields.String, 'created_at': fields.DateTime, 'user_id' : fields.Integer