0

i had used an sample web form "index.html"(it is in templates folder) in which it contains a text box to enter email .then the data should be posted to sample.py and it should be printed.but it is not happening,it simply showing 404 not found after clicking signup in web form.here is my code,please correct me if i am wrong ,and also please tell me how to run this in pycharm 4.5.i am a beginner. please help me.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="/signup" method="post">
  <input type="text" name="email">
  <input type="submit" value="Signup">
</form>
</body>
</html>

my python code

from flask import Flask,request,redirect

app = Flask(__name__)

@app.route('/signup', methods = ['POST'])
def signup():
    email = request.form['email']
    print("The email address is '" + email + "'")
    return redirect('/')
9
  • Check Your action="" Commented Jul 29, 2015 at 13:16
  • yaa,action="/signup" is it wrong,if it is wrong what it should be??,it means that the data should be posted to the signup method,am i right???? Commented Jul 29, 2015 at 13:18
  • does my question is clear or should i add any more @Daniel Roseman sir Commented Jul 29, 2015 at 13:25
  • What's displaying the HTML template in the first place? Commented Jul 29, 2015 at 13:33
  • my index.html is in the templates folder,sir i could not get u what exactly you are asking,please explain,i am just beginner Commented Jul 29, 2015 at 13:37

2 Answers 2

1

In the code that you have posted there is no route or handler registered for /, however, signup() redirects to /. Thus you will always see a 404 error if you post to http://localhost:5000/signup (assuming that is the address of your Flask server).

Posting to `/signup' should result in the print message being displayed on your console. If that is happening then at least you know that the Flask server is working.

You should implement a handler for the / route; perhaps rendering index.html:

from flask import Flask,request,redirect
from flask import render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')

@app.route('/signup', methods = ['POST'])
def signup():
    email = request.form['email']
    print("The email address is '" + email + "'")
    return redirect('/')

app.run()

Now the redirect from the signup page should not result in 404 errors.

Run code (python app.py), then you can load http://localhost:5000 directly in your browser, and you should see the signup page displayed. Enter an email address and click "Signup". The text that you entered should be printed to the console in which you started the Flask server, and your browser should redirect back to the index page.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks bro,it worked,can u help me in referring any documentations in coming up with this topics
If you haven't already read it, flask.pocoo.org/docs/0.10/quickstart should get you up and running.
problem was fixed,and is there any chance of printing them in web page it self
Displaying the email address on the web page requires you to write a template that has placeholders for the data that you want to display (in this case email address), and to pass the data to render_template() - you can read about that here.
@learner yeah you can print it on the web page by flashing it
1

try action = "{{url_for('signup'}}" also when you use print in flask it's seen on the console not the webpage

3 Comments

sir, it did not work ,the output on console is empty
launch flask first also try methods = ["GET","POST"]`
sir,i am working on this code in flask project it self

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.