Question:
da title. An example would be there’s a signup page, and when you finish da form it redirects you to another route where your account gets created. How do I do that?
(sorry for the unclear explanation, I got notifications about a fake virus few minutes ago)
Repl link:
no repl link or code (cause I’m just wondering how to do it)
for the purpose you provided in your original post, you would create a form using the <form>
tag and a parameter that tells the browser to POST
to an endpoint on your flask server that accepts POST
requests.
Umm… ok…
So can you provide an example?
File structure:
main.py
templates
| register.html
Python main.py
:
from flask import Flask, render_template, request
app = Flask('app')
@app.route('/register')
return render_template('register.html')
@app.route('/register/create', methods=['POST'])
username = request.form['username']
password = request.form['password']
#account creation stuff goes here
return 'Account created!'
app.run(host='0.0.0.0', port=8080)
HTML register.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create Account</title>
</head>
<body>
<form action="/register/create" method="POST">
<input type="username" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Signup">
</form>
</body>
</html>
Can I do this without forms?
What do you mean by that?
Like maybe there’s 2 buttons, and the user has to choose one of them.
Try using a radio form element.
<p>Pick a color:</p>
<input type="radio" name="color" value="red"> Red
<input type="radio" name="color" value="green"> Green
<input type="radio" name="color" value="blue"> Blue
<input type="submit" value="Submit">
Shouldn’t this still be in a form?
Nope, that’s not how you use a decorator…
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'GET': return render_template('register.html')
username = request.form['username']
password = request.form['password']
# account creation stuff goes here
return 'Account created!'
if __name__ == '__main__':
app.run('0.0.0.0', 81)
I could use args… (/route?arg1=val1&arg2=val2
), but doesn’t that go in people’s history?
Yeah NEVER pass passwords in the URL. Why do you need to do it without forms anyway?
then what’s the HTML?
i just wrote this from memory without testing it.
(its also very late for me)
The action would just be /register
<form action="/register" method="POST">
<input type="username" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit">Create account</button>
</form>
same
ok it’s only 10pm for me
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.