flask中的Flask、request、render_temple、redirect和url_for

Science flask is also more than a week, this web framework also have a little understanding, combing some basic knowledge, or the white one, the code written relatively low, if the article had the wrong place at any time chiefs welcome correction code are commented out code is on the prevention csrf, you can ignore

The main program script:

. 1  from Flask Import Flask, the render_template, Request, the redirect, the url_for
 2  
. 3  # from flask_wtf Import CSRFProtect 
. 4  
. 5 App = Flask ( the __name__ ) # Declare a Flask class __ effect name__ parameter is to determine the root of the program, so as to obtain template file static files
 . 6  # the app.config [ "of SECRET_KEY"] = "12345678" 
. 7  #
 . 8  # CSRFProtect (App) 
. 9  
10  
. 11 @ app.route ( ' / ' ) # @ app.router () is a decorator its role is to try to function (can be simply understood achievement is below it functions) and one url (section later in brackets) binding, when accessing this url, will run the view function
 12  DEF Helloworld ( ):
 13     return the redirect (the url_for ( ' Home ' )) to view the url_for inverting function, the first parameter is a function name view, if the view function has parameters, can be added at the end, returns url, it is redirected to the redirect url inside the brackets , where the return value is url_for
 14  
15  
16 @ app.route ( " / REGIST / " , methods = [ " get " , " POST " ]) # methods assignment request url this method, default get, get used herein designated or post in two ways
 . 17  DEF REGIST ():
 18 is      IF request.method == " the GET " :
 . 19          return the render_template ( " csrf_regist.html ") # Render_template to render the page, if the page to be accepted parameter is present, the parameters may be on the back
 20 is  
21 is      the else :
 22 is          Print (Request.Form) # mode if the request is a post, then the output of the user input information in the background. can get to the front-end request input by the user, acquiring request.args get request, Request.Form post acquisition request
 23 is  
24          return the redirect (the url_for ( ' login ' )) # login view of function inversion, and redirects
 25  
26 is  
27 @ app.route ( " / Login / " , Methods = [ " GET " , " POST " ])
 28  DEF Login ():
 29      IF request.method == " the GET":
30         return render_template("csrf_login.html")
31 
32     else:
33         print(request.form)
34 
35         return redirect(url_for('home'))
36 
37 
38 @app.route('/home/')
39 def home():
40     return render_template('csrf_home.html')
41 
42 
43 if __name__ == "__main__":
44     app.run()

 

Home (csrf_home.html) Code

. 1 <! DOCTYPE HTML>
 2 <HTML lang = " EN " >
 . 3 <head>
 . 4      <Meta charset = " UTF-. 8 " >
 . 5      <title> CSRF Home </ title>
 . 6 </ head>
 . 7 <body>
 . 8 <h1> Welcome to the home page, select log in or register </ h1>
 9 <a href= "{{ url_for('login') }}"> landing </a> # when the user clicks landing, jump to landing page
 10 <a href= "{{ url_for('regist') }}"> Sign </a>
# When the user clicks registered, the registration page jump to 11 </ body>
 12 </ HTML>

 

Registration (csrf_regist.html) Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>csrf的注册页面</title>
</head>
<body>
    <form action="", method="post">
        <table>
            <tbody>
{#                <td>#}
{#                    <input type="text" name="csrf_token", value="{{ csrf_token() }}">#}
{#                </td>} # 
                    <TR> Username: </ tr>
                <TD>
                    <tr><input type="text" name="username" value=""></tr><br>
                </td>
                <td>
                    <tr>密码:</tr>
                    <tr><input type="password" name="password" value=""></tr><br>
                    <input type="submit" value="注册">
                </td>
            </tbody>
        </table>
    </form>
</body>
</html>

 

Landing (csrf_login.html) Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>csrf登陆页面</title>
</head>
<body>
    <form action="", method="post">
        <table>
            <tbody>
{#                <td>#}
{#                    <input type="text" name="csrf_token", value="{{ csrf_token() }}">#}
{#                </td>} # 
                    <TR> Username: </ tr>
                <TD>
                    <tr><input type="text" name="username" value=""></tr><br>
                </td>
                <td>
                    <tr>密码:</tr>
                    <tr><input type="password" name="password" value=""></tr><br>
                    <input type="submit" value="登陆">
                </td>
            </tbody>
        </table>
    </form>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11199475.html