Showing posts with label flask. Show all posts
Showing posts with label flask. Show all posts

Monday, January 17, 2022

[flask][Resolved] ValueError: urls must start with a leading slash

Error message:

ValueError: urls must start with a leading slash

Source

from flask import Flask, Blueprint
mod = Blueprint('apis', __name__, url_prefix='api/valuations')

Correction

from flask import Flask, Blueprint mod = Blueprint('apis', __name__, url_prefix='/api/valuations')

Friday, November 10, 2017

[Flask][Resolve] no module named 'flask_mysql'


 Error message

app.py:2: ExtDeprecationWarning: Importing flask.ext.mysql is deprecated, use fl
ask_mysql instead.
  from flask.ext.mysql import MySQL
C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\fl
ask\exthook.py:106: ExtDeprecationWarning: Detected extension named flaskext.mys
ql, please rename it to flask_mysql. The old form is deprecated.
  .format(x=modname), ExtDeprecationWarning
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Jan/2018 14:49:11] "GET / HTTP/1.1" 200 -

 Source code

from flask import Flask, render_template, json, request
from flask.ext.mysql import MySQL

mysql = MySQL()
app = Flask(__name__)
mysql.init_app(app)

@app.route('/')
def main():
    return render_template('index.html')
  
if __name__ == "__main__":
    app.run(port=5002)
Error message shows "flask.ext.mysql is deprecated, use flask_mysql instead.", followed the instruction use flask_mysql still fail :

 Wrong code follow instrument from error message:
from flask import Flask, render_template, json, request
from flask_mysql import MySQL

mysql = MySQL()
app = Flask(__name__)
mysql.init_app(app)

@app.route('/')
def main():
    return render_template('index.html')
 
if __name__ == "__main__":
    app.run(port=5002)
Finally find need to import flaskext.mysql:
from flask import Flask, render_template, request, json
from flaskext.mysql import MySQL

app = Flask(__name__)
mysql = MySQL()
mysql.init_app(app)

@app.route("/")
def main():
    return render_template('index.html')

if __name__ == "__main__":
    app.run()

Reference

http://flask-mysql.readthedocs.io/en/latest/
https://github.com/jay3dec/PythonFlaskMySQLApp---Part-1

[Flask][Resolved] NameError: name 'request' is not defined

Error message

C:\python\flask\tutsplus.com>python app.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2018-01-11 12:38:56,004] ERROR in app: Exception on /signUp [POST]
Traceback (most recent call last):
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "app.py", line 14, in signUp
    _name = request.form['inputName'] # read the posted values from the UI
NameError: name 'request' is not defined
127.0.0.1 - - [11/Jan/2018 12:38:56] "POST /signUp HTTP/1.1" 500 -

Example source code

from flask import Flask
app = Flask(__name__) #create an app using Flask as shown

@app.route('/',methods=['POST'])
def main():
    _name = request.form['inputName'] # read the posted values from the UI
    _email = request.form['inputEmail']
    _password = request.form['inputPassword']
    
if __name__ == "__main__": #check if the executed file is the main program
    app.run() #run the app

Solution


Since request is used, so you need to check request is imported:
from flask import Flask, request
app = Flask(__name__) #create an app using Flask as shown

@app.route('/',methods=['POST'])
def main():
    _name = request.form['inputName'] # read the posted values from the UI
    _email = request.form['inputEmail']
    _password = request.form['inputPassword']
    
if __name__ == "__main__": #check if the executed file is the main program
    app.run() #run the app