Reputation: 9
i want to be able to get the the user name from a form and append it to a confirmation page but it keeps returning none
below is the html form
<form name="sentMessage" id="contactForm" action="{{ url_for('contact') }}" method="post" novalidate>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Name</label>
<input type="text" name="name" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Email Address</label>
<input type="email" name="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" name="phone" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" name="message" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<button type="submit" class="btn btn-primary" id="sendMessageButton">Send</button>
</form>
and the flask code is below
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/form-entry", methods=["GET", "POST"])
def receive_data():
if request.method == "POST":
name = request.form.get("name")
return f"<h1>{name} Successfully sent your message</h1>"
i tried using request.form["name"] but it comes back with a bad request error
Upvotes: 0
Views: 416
Reputation: 18968
The template as defined, when rendered through the /contact
endpoint, the following line (unrelated attributes replaced with ellipsis):
<form ... action="{{ url_for('contact') }}" method="post" ...>
Renders to
<form ... action="/contact" method="post" ...>
Submitting the form will again hit the same /contact
endpoint, which does not accept the POST
method as the default list of accept methods
was used for that. This resulted in the Bad Request error as reported.
Given the intended target is supposed to be the received_data
function, hooked to the /form-entry
endpoint, the intended argument for url_for
in the template should be 'receive_data'
, i.e.
<form ... action="{{ url_for('receive_data') }}" method="post" ...>
which would result in this rendering:
<form ... action="/form-entry" method="post" ...>
When submitting that form, the intended end-point should be used and the message should be rendered without issues.
Upvotes: 1