I am planning to get input from an HTML Form when submitted, the input will be send over to Python. Here is the HTML File
<form method="GET">
<div class="form-inline">
<div class="form-group">
<input type="email" class="line-input" name="userEmail" placeholder="Email">
</div>
<div class="form-group">
<input type="password" class="line-input" name="userPassword" placeholder="Password">
</div>
</div>
<div class="form-inline">
<div class="form-group">
<input type="email" class="line-input" name="recipientMail" placeholder="Recipient">
</div>
<div class="form-group">
<input type="email" class="line-input" name="CCEmail" placeholder="CC">
</div>
</div>
<div class=" form-group">
<button type="submit" class="btn btn-light text-primary btn-block" style="margin : 20px 20px -10px 0px">Send Message</button>
</div>
</form>
Now I don't know the best way to do this, I tried this in Python but doesn't seem to work
#app.route('/', methods=['POST'])
def form_post():
userEmail = request.form['userEmail']
userPassword = request.form['userPassword']
return userEmail, userPassword
Can anyone help me out here?
Change your form method from GET to POST, as your route only specifies "POST", and will not accept any other requests of a different type:
<form method="POST">
Edit: if you wish to specify both methods, ensure that your route checks for the correct type of request currently being sent when the route is triggered:
#app.route('/', methods=['POST','GET'])
def form_post():
if flask.request.method == 'POST'
userEmail = request.form['userEmail']
userPassword = request.form['userPassword']
return userEmail, userPassword
return flask.render_template('something.html')
Note, however, that you are creating your form on the home route ('/'). It may be best to return a link to the page that has the form code:
#app.route('/')
def home():
return 'Welcome! login here'
#app.route('/login', methods=['GET', 'POST']):
if flask.request.method == 'POST'
userEmail = request.form['userEmail']
userPassword = request.form['userPassword']
return flask.redirect('/')
return flask.render_template('form_filename.html')
Related
I am trying to execute a form POST request. The request is received at the route api function but the POST condition is not executing.
Code
<form class="pt-2 pb-4" action = "{{ url_for('data_sources_api.testfn') }}" method = "POST">
<div class="row">
<div class="col-md-4 mb-3">
<label for="url" class="form-label">URL</label>
<input type="text" class="form-control" id="url" name="url" aria-describedby="emailHeurllp"
placeholder="Enter new events top domain source url">
</div>
<div class="col-md-4 mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Organization Name">
</div>
<div class="col-md-12">
<button type="button" class="btn btn-secondary mr-2" value="submit" name = "submit">Submit</button>
</div>
</div>
</form>
Flask Code
#data_sources_api.route('/login/test', methods=["GET", "POST"])
def testfn():
# Check if user is loggedin
if loggedin():
# User is loggedin, render the home page
if request.method == 'POST':
print("POST Request Received")
result = request.form
return render_template('users/data-sources.html', role=session['role'])
return redirect(url_for('account_api.login'))
The request received is GET when i click the submit button and not POST :(
Thanks in advance
I think the problem is with button. Replace the button with input type="submit":
<input type="submit" value="Submit">
Here is the example of Django form.
I have a user registration form which has a select tag. I want the value from the selected option to be assigned to a variable(the code is shown below).
registration.html
<form method="POST" action="/register">
<div class="form-group">
{{render_field(form.name, class_="form-control", placeholder="Name")}}
</div>
<div class="form-group">
{{render_field(form.email, class_="form-control",placeholder="Email")}}
</div>
<div class="form-group">
{{render_field(form.username, class_="form-control", placeholder="Username")}}
</div>
<div class="form-group">
{{render_field(form.password, class_="form-control", placeholder="Password")}}
</div>
<div class="form-group">
{{render_field(form.confirm, class_="form-control", placeholder="Re-enter Password")}}
</div>
<div class="form-group">
<p>I prefer to : </p>
<select id= "studenttype" name="studenttype">
<input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
<input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>
</select>
</div>
<input type="submit" class="btn btn-success" value="Submit">
<p>Register as Instructor </p>
</form>
I have used the following code to assign the selected option value to the variable learning_style.
app.py
#app.route('/register', methods=['GET', 'POST'])
def register():
learning_style = request.form.get("studenttype")
form = RegisterForm(request.form)
if request.method == "POST" and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = form.password.data
user_type = 'student'
print(learning_style)
...........
flash('You are now registered', 'success')
redirect(url_for('index'))
return render_template('register.html', form=form)
But this doesn't seem to work. Any help is much appreciated. Thanks.
You are mixing up radio buttons and select dropdowns.
Change this in your template from:
<select id= "studenttype" name="studenttype">
<input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
<input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>
</select>
to:
<input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
<input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>
And in your app:
learning_style = request.form.get("Theorist")
I have problem with using multiple forms at once, tried with loop count in name but when i check correct options and then submit I get this error:
The browser (or proxy) sent a request that this server could not understand.
Code:
{%for tournament in tournament_query%}
<div class="body-main">
<div class ="body-select">
<form action="", method="POST">
<input type="radio" name="option" value="{{tournament.player_one}}"> {{tournament.player_one}}<br>
</form>
</div>
<div class ="body-selected">
<form action="", method="POST">
<input type="radio" name="option" value="{{tournament.player_two}}"> {{tournament.player_two}}<br>
</div>
<input type="submit" value="Submit">
</form>
</div>
{%endfor%}
my flask route =
#app.route("/news", methods=['POST', 'GET'])
#login_required
def news():
tournament_query = Walka.query.join(Walka.Tournament).filter_by(name = 'Das').all()
if request.method == "POST":
option = request.form['options']
return option
return render_template("news.html", tournament_query = tournament_query)
I want to get value from every one but I don't know how I can acquire it
I'm making a text based browser game in python and flask but I encountered a strange problem.
When I try to login on my website I get a "bad request" error.
This is my login route:
#app.route('/login/', methods=['GET','POST'])
def login():
if current_user.is_authenticated:
return redirect("/game/", code=302)
# when the form is filled in
if request.method == 'POST':
# register the user
if request.form['regSubmit'] == 'regSubmit':
username = request.form['regUser']
password = request.form['regPassword']
address = request.form['regAddress']
register_user(username=username,password=password,address=address)
print(address)
return redirect("/login/", code=302)
# login user
if request.form['logSubmit'] == 'logSubmit':
print('lol')
username = request.form['logUser']
password = request.form['logPassword']
result = users.find_one({"user": username})
if result and check_password_hash(result['password'], password):
user_obj = User(result['_id'])
login_user(user_obj)
return redirect("/game/", code=302)
return render_template('out.html')
This is my login modal:
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Login</h4>
</div>
<form method="post">
<div class="modal-body">
<p>Please enter your username and password.</p>
<div class="form-group">
<label class="control-label" for="logUser">Username</label>
<input type="text" name="logUser" class="form-control" id="logUser">
</div>
<div class="form-group">
<label class="control-label" for="logPassword">Password</label>
<input type="password" name="logPassword" class="form-control" id="logPassword">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" name="logSubmit" value="logSubmit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
I believe your code is failing because you're trying to access request.form['regSubmit'], but this field does not exist in your form. Accessing any form fields that do not exist will cause Flask to return a 400 Bad Request error.
To get around this, you can either submit your login and register actions to different Flask views, or you can use a try...except block to catch the KeyError generated by trying to access a non-existent form field.
I have this form and every time I try to post it to the method in the buttom of the Question. The application just gives me a 405 error directly. I have checked that my database works by using the same method in the console.
(I have removed alot of html because it was just styling and stuff like that)
<form action="/register" method="post" class="grid">
<div class="cell colspan3" style="margin-left:2%;">
<div class="input-control modern text">
<input type="email" name="email" id="email">
<span class="label">Email</span>
<span class="informer">Skriv din email</span>
<span class="placeholder">Email</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="text" name="username" id="username" >
<span class="label">Brugernavn</span>
<span class="informer">Skriv dit brugernavn</span>
<span class="placeholder">Brugernavn</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="password" name="password" id="password">
<span class="label">Password</span>
<span class="informer">Skriv dit password</span>
<span class="placeholder">Password</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="password" name="passwordconfirm" id="passwordconfirm">
<span class="label">Gentag password</span>
<span class="informer">Skriv dit password igen.</span>
<span class="placeholder">Gentag password</span>
</div>
</div>
<div class="cell colspan3">
<label class="input-control checkbox ">
<input type="checkbox" name="accept_tos" id="accept_tos">
<span class="check"></span>
<span class="caption">Jeg accepterer hjemmesidens regler</span>
</label>
</div>
<div class="cell colspan3">
<div class="input-control cell">
<button class="button success" type="submit">Registrer</button>
</div>
</div>
</div>
</div>
</form>
The python method that I call with my html post form:
#app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
newUser = User(form.username.data, form.email.data, form.password.data)
db.session.add(newUser)
db.session.commit()
return render_template('login.html')
else:
return render_template('register.html')
And the RegistrationForm method looks like this:
class RegistrationForm(Form):
username = StringField('username', [validators.Length(min=4, max=25)])
email = StringField('email', [validators.email, validators.Length(min=4, max=40)])
password = PasswordField('password',
[validators.Required(),
validators.EqualTo('confirm', message='Passwords skal matche hinanden.')
])
confirm = PasswordField('passwordconfirm')
accept_tos = BooleanField('Jeg accepterer regler for denne hjemmeside', [validators.Required()])
obviously there'snt any problem in these codes unless you have url_prefix for your current app like :
app = Blueprint('user', __name__, url_prefix='/user/')
and u have another blueprint with url_prefix like :
app = Blueprint('general', __name__, url_prefix='/')
with another funtion like :
#app.route('/register', methods=['GET'])
in the blueprint ...
and your form is posting data to '/register' instead of '/user/register'
If that really is the code you're using, I can't see why you're getting a 405 Unsupported Method rather than a validation error. You need to render the hidden CSRF field in the html form.
<form ...>
{{ form.hidden_tag() }}
There are other things wrong with this code. Redirect to login, don't render the template from the register view. Use form.validate_on_submit(). Don't pass request.form to the form. Let the form render the inputs, don't write the html yourself.
Try changing
if request.method == 'POST' and form.validate():
to
if form.validate_on_submit():