Flsak Study Notes (1)

Day 01

Recent projects written in python to use the back end, the students recommended the flask to learn a framework to learn. The main purpose of writing this blog is to record what content they learn, do not forget to have a basic knowledge of one to Baidu, there is to share with you, where there is not very easy to understand Comments can learn new things like this, Society is not to say that can understand, but completely new to people who did not feel this way. Here IDE is not very recommended to use the Community Edition, Professional Edition because you can directly select New Project flask, auto-generated frame-based projects (although the impact is not great).

Chinese New Year at home, learning efficiency is really low, said a lot of nonsense, or remember the point of learning content, incidentally, I wish you all a Happy New Year.

Four open debug mode

  1. app.run(debug=True)

  2. app.debug=True

  3. app.config.update(DEBUG=True)

  4. Using the configuration file in two ways: (config.py need to create a file)

    1. Use import import using app.config.from_object (config)

    2.app.config.from_pyfile ( 'file_name', silent = False), without having to import, the file format may be .py, and the like may be .txt, silent time = False If the file does not exist will be given

Passing parameters

@app.route('/path/<type:arg>/')
#type:int,float,string,path,uuid,any...
def fun(arg):
   pass

 

Each app.route represents a view, if the variable needs to be placed in <>, the variables can be "Type: variable name" explicitly specify the type

string type can only accept free \ / string

acceptable path can comprise \ / string

any path can specify multiple

#example for any:
@app.route('/<any(blog,user):url_path>/<id>')
#../blog/1
#../user/1
#都可以访问
def fun(url_path,id):
   if url_path == 'blog':
       return 'blog detail'
   else:
       return 'user detail'

If there is uncertainty parameters, you can pass parameters by a question mark. A plurality of connection parameters between an ampersand.

 

? # .. / d / WD = ... 
@app. route ( '/ d /')
DEF Fun (): Arg = Request. args. GET ( 'arg_name') return 'parameter you are looking for is% s' % Arg
   
   

 

url_for function is a very common function, which function is to return a view corresponding path.

Use method: the url_for ( 'endpoint', ** = value), note that this endpoint view function name corresponding to the function, not the end flag to see the name understood as the path, such as a return like '/ d /' of path, endpoint corresponds to 'fun' instead of 'd'

Its advantage is that, when the endpoint corresponding path can be changed individually without looking for changes

#example
@app.route('/page/<string:pg>')
def page(pg):
   return pg
@app.route('/')
def home_page():
   return 'URL for page1 is '+url_for('page',pg='page1')

Guess you like

Origin www.cnblogs.com/Ishtarin/p/12232439.html