Day 080 - Project 80 : Actually logging in!

If you have any questions, comments or issues with this project please post them here!

The ā€œFix my codeā€ part is missing two errors in the solution, so the solution is not executable.

Line 5: Missing an ā€œ@ā€ in front
Line 11: ā€œDavidā€ needs a capital ā€œDā€

1 Like

Could you sumarize the lesson? I need to use the skill but haven’t made it to this one.

You could go to the Replit YouTube Channel.
Day 80 Video

2 Likes

Thanks, I forgot about that :smiley:

I take it back I’m on like day 40 so I don’t know any html or what flask is

from flask import Flask, request

app = Flask(__name__, static_url_path="/static")

@app.route('/login', methods=["POST"])
def prosess():
  page=""
  form = request.form
  if form["Username"] == "Gaurav291" and form["Password"] == "p23anther":
    page += """<html>
    <head>
    <title>Logged In</title>
    <link href="/static/Css/style_l.css" rel="stylesheet" >
    </head>
    <body>
    <h1>You are logged in </h1>
    </body>
    </html>"""
  elif form["Username"] == "Gaurav591" and  form["Password"] == "p23anther291": 
    page += """<html>
    <head>
    <title>Logged In</title>
    <link href="/static/Css/style_l.css" rel="stylesheet" >
    </head>
    <body>
    <h1>You are logged in </h1>
    </body>
    </html>"""
  elif form["Username"] == "Vinay291" and form["Password"] == "p23anther591":
    page += """<html>
    <head>
    <title>Logged In</title>
    <link href="/static/Css/style_l.css" rel="stylesheet" >
    </head>
    <body>
    <h1>You are logged in </h1>
    </body>
    </html>"""
  else:
    page += """<html>
    <head>
    <title>Logged In</title>
    <link href="/static/Css/style_r.css" rel="stylesheet" >
    </head>
    <body>
    <h1>Incorrect Passward and Username</h1>
    </body>
    </html>"""
  return page

@app.route('/')
def index():
  page = """<!DOCTYPE html>
<html>
  <head>
    <titl>Login Page</titl>
    <link href="/static/Css/style.css" rel="stylesheet">
  </head>
  <body>
    
    <h1>Login</h1>
    <form method="post" action="/login">
      <p>Username: <input type="text" name="Username" required></p>
      <p>Email: <input type="Email" name="Email"></p>
      <p>Password: <input type="Password" name="Password" required></p>
      <button type="submit">Login</button>
    </form>
  </body>
</html>"""
  return page

app.run(host="0.0.0.0", port="81")`

This Code can also be written in this form ( is this is right)

Yes, you can write the HTML inside main.py. Kind of unreadable imo though.

1 Like

Question: I have set up a Flask server with the correct app route and linked my HTML file with that correctly I beleive. still, the server is not running. Afaiu code is fine. Please guide me as I am beginner in programming


Repl link: https://replit.com/@mayankk985/Day80100Days#main.py

from flask import Flask, request

app = Flask(__name__)


@app.route("/process", methods=["POST"])
def process():
    form = request.form
    page = ""

    if form["baldies"] == "david":
        page += f"You're alright {form['username']}"
    else:
        page += f"You've picked wrong {form['username']}"
    return page


@app.route("/")
def index():
    page = ""
    f = open("index.html", "r")
    page = f.read()
    f.close()
    return page


app.run(host="0.0.0.0", port=81)

<!DOCTYPE html>
<html>
  
  <body>
  <form method = "post" action="/process">
    <p>Name: <input type="text" name="username" required> </p>
    <p>Email: <input type="Email" name="email"> </p>
    <p>Website: <input type="url" name="website"> </p>
    <p>Age: <input type="number" name="age"> </p>
    <p><input type="hidden" name="userID" value="232"> </p>

    <p>
      Fave Baldy: 
      <select name="baldies">
        <option value ='david'>David</option>
        <option value= 'luc'>Jean Luc Picard</option>
        <option value='yul'>Yul Brynner</option>
      </select>
    </p>

    <button type="submit">Save Data</button>
  </form>
  
  </body>
  </html>

Hi @mayankk985 interesting error message in your code (keep it SFW please, as per Replit Ask Community Standards). When I fork the Repl it works fine for me, asks for a username, email and password.

Do you still see the same error when you run your Repl?

2 Likes

Hey @IanAtCSTeach I am sorry about that msg,I was going crazy while debugging it, I didn’t notice that before posting the link herešŸ™ƒ. unfortunately it’s still not working. I don’t get any error message on console, however it not opening in webview or on browser. on the browser/ webview it says ’ It looks like day80100days.mayankk985.repl.co closed the connection’

Try using

app.run(host='0.0.0.0', port=8080) 

instead of port 81. Replit might be using the other ports.

1 Like

As far as I’m aware, pid1 (Replit’s init program, which is the only program that Replit runs inside repls that accepts TCP connections, other than a VNC server, which listens on port 5900 and is only started when graphical programs are used) currently only listens on the following addresses:

  • 127.0.0.1:8283
  • 0.0.0.0:22

If I missed any, feel free to edit this.

1 Like

It seems like your replit account doesn’t have a TLS certificate (for *.mayankk985.repl.co) right now, try waiting a while.

1 Like

After day 80 I tried to write a program where I used Pyjokes library with a simple form (to select the language), however once inside the form I’m struggling to make the ā€œnext oneā€ (reload) button work. Could anyone help me, please? :pray:t2:

https://replit.com/@Sibelius11/Jokesa

I have managed to do the form by myself for a set of data for one user but had to look at the solution for the dictionary of data and still didn’t manage to get the return for login to say ā€œWelcome {USER}ā€ with the {USER} being from the form.user

I’m not sure what I’m missing in my solution. I’ve checked it and triple-checked it for pathway errors, extra commas, hidden capitals, missing indents, and all the rest but my process subroutine is not returning the messages in new webpages. Everything else works: the CSS and the HTML and the login subroutine, but there’s something about the process that’s off because realAccounts = True has a warning on it that says it’s defined by not locally used.

Anyways, here’s what I’m working with:

 from flask import Flask, request

app = Flask(__name__, static_url_path='/static')

accounts = {}

accounts["username1"] = {"email": "username1@gmail.com", "password": "password1"}
accounts["username2"] = {"email": "username2@gmail.com", "password": "password1"}
accounts["username3"] = {"email": "username3@gmail.com", "password": "password3"}


@app.route("/process", methods=["POST"])
def process():
  form = request.form
  
  realAccount = False
  details = {}
  try:
    details = accounts[form["username"]]
    realAccount = True
  except:
    return "Username, email, or password incorrect. Try again."
  if form["email"] == details["email"] and form["password"] == details["password"]:
    return "Login successful."
  else:
    return "Username, email, or password incorrect. Try again."
  

@app.route('/')
def login():
  css = "login.css"
  
  page = ""
  with open("static/html/login.html", "r") as f:
    page = f.read()

  page = page.replace("{css}", css)
  return page
  
app.run(host='0.0.0.0', port=81)

HTML:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>My Website</title>
  <link href="/static/css/{css}" rel="stylesheet" type="text/css" />
</head>

<body>
  <h1>Login to your account</h1>
  <form method="post" action="/process">
    <p><label>Username: </label><input type="text" name="username" required></p>
    <p><label>Email: </label><input type="email" name="email" required></p>
    <p><label>Password: </label><input type="password" name="password" required></p>

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

</html>

Anyone have an ideas?

Shouldn’t need to unless the error message tells you to

Wait what’s the actual warning?

Alright, after much troubleshooting—spending hours and hours messing with all parts of the code, defining additional subroutines to handle dafaulted get methods, pouring over developer tools’ network error messages, gnashing my teeth!—I finally found my answer. It turns out that all I had to do was bring the URL of the webpage out from within Replit’s web viewer tab and into my main browser…
:face_with_spiral_eyes:

Thanks, I’m realizing that slowly. I’m still fairly new to programming so all the back-end operations are a labyrinth to me (I couldn’t even tell you what I mean by that yet).

I ended up getting rid of the realAccount variable to no apparent detriment to my program, but the message was ā€œlocal variable ā€˜realAccount’ is assigned to but never used.ā€