JSON not writing (flask app)

Question:
For some reason, the JSON data is not writing. I have tried JSON writing and it worked, so the request is not functioning.

Repl link: https://replit.com/@NateDhaliwal/Flask-forum?v=1

main.py:

@app.route('/login')
def login():
  return render_template('login.html')
  file=pathlib.Path('passwords.json')
  username=request.form.get('email')
  password=request.form.get('password')
  loginInfo = {'u': username, 'p': password}
  file.write_text(json.dumps(loginInfo))

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login</title>
</head>
<body>
  <form action="/forum">
    <h1>Login</h1>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" required>
    <br>
    <label for="password"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" required>

    <button type="submit">Login</button>
  </form>
</body>
</html>

home.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Forum Home</title>
</head>

  <body>
  <h1>Welcome to NDForum!</h1>
  <p>Login to get started:</p>
  <a href="/login">Login</a>

</body>
</html>

Please help guys!!!

1 Like

Try this code:

@app.route('/login', methods=['GET', 'POST'])  # You should specify 'POST' method for form submission
def login():
    if request.method == 'POST':
        username = request.form.get('email')
        password = request.form.get('password')
        
        loginInfo = {'u': username, 'p': password}
        
        # Open the file in 'w' mode to write data
        with open('passwords.json', 'w') as file:
            json.dump(loginInfo, file)
        
    return render_template('login.html')
3 Likes

will never run because the only method is POST

1 Like

I don’t think you know Flask properly then because, yes, that will run. I have no clue where you even got that idea from. The return render_template('login.html') is at the end of the if statement; therefore, in that context, it means if the request method isn’t POST , it will return the login template. Also, if you just define POST as the method, it will automatically add GET as well.

Nope, that’s not how Flask works. Try it yourself. It will give Method Not Allowed if you go to /login.

1 Like

I did try it; I do the same exact thing for some of the routes in my image hoster, and it works perfectly fine.

EDIT: I stand corrected; it does not work. I was looking at it wrong; my bad. Yeah, just include “GET” in the methods, and it will work, lol. I thought it was like a backend API; that’s why I only put POST and not GET. My code was still correct, just not in this context. In this context, /login is a frontend that’s simply sending a POST request to the same URL path and retrieving the form data, and then performing whatever is intended afterwards.

2 Likes

Any ideas @Sky @QwertyQwerty88 ? I can’t continue without this…

1 Like

Did you try any of the methods me and qwerty mentioned?

1 Like

Yes, and there’s absolutely nothing inside passwords.json.

Is Flask even getting form data?

When you use <form action="/forum" method="post">, then it posts to /forum but your code to handle it is at /login. This means that none of the code that writes it to the json is executing.
Also, you currently have the forum route only accepting POST requests but you’re trying to return an html page which won’t work as it will return a 405 (Method not allowed error).

Try this code: https://replit.com/@CosmicBear/Flask-forum

Another thing, you currently have with open('passwords.json', 'w') as file:. The W means “write” and it basically tells the program to write to the file. This works if you want it to re-write the file every time but if you want it to add new information and also keep old information then this won’t work.

2 Likes

Ok guys, I found the Solution myself: put the request and the writing in the /forum route, not /login.
P.S. This was before I checked @CosmicBear’s repl. I checked it just before I wrote this.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.