ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Flask, form 예제
    카테고리 없음 2023. 5. 28. 17:15
    <!DOCTYPE html>
    <html>
    <head>
        <title>Form 1</title>
    </head>
    <body>
        <h1>Form 1</h1>
        <form method="POST" action="/form1">
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" required>
            <br><br>
            <label for="email">Email:</label>
            <input type="email" id="email" name="email" required>
            <br><br>
            <input type="submit" value="Submit">
        </form>
    </body>
    </html>

     

    <!DOCTYPE html>
    <html>
    <head>
        <title>Form 2</title>
    </head>
    <body>
        <h1>Form 2</h1>
        <form method="POST" action="/form2">
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" required>
            <br><br>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" required>
            <br><br>
            <input type="submit" value="Submit">
        </form>
    </body>
    </html>
    from flask import Flask, request, render_template
    
    app = Flask(__name__)
    
    @app.route('/form1', methods=['GET', 'POST'])
    def handle_form1():
        if request.method == 'POST':
            name = request.form.get('name')
            email = request.form.get('email')
            print(name, email)
            # Process the form data for form1
            return f"Form 1: Name - {name}, Email - {email}"
        return render_template('form1.html')
    
    @app.route('/form2', methods=['GET', 'POST'])
    def handle_form2():
        if request.method == 'POST':
            username = request.form.get('username')
            password = request.form.get('password')
            print(username, password)
            # Process the form data for form2
            return f"Form 2: Username - {username}, Password - {password}"
        return render_template('form2.html')
    
    if __name__ == '__main__':
        app.run(debug=True, host='192.168.168.163', port=5001)
Designed by Tistory.