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ā
Could you sumarize the lesson? I need to use the skill but havenāt made it to this one.
Thanks, I forgot about that
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.
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?
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.
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.
It seems like your replit account doesnāt have a TLS certificate (for *.mayankk985.repl.co
) right now, try waiting a while.
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?
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ā¦
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.ā