from flask import Flask, request, redirect
app = Flask(__name__)
#app.route('/signup', methods=['POST'])
def signup():
email = request.form['email']
print("Adresa de email este: " + email)
return redirect('/')
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="/signup" method = "post">
<input type = "text" name = "email"></input>
<input type = "submit" value = "Signup"></input>
</form>
</body>
</html>
So I m trying to print the email that I m writing in the html form but I get the following error:
" Your file couldn’t be accessed.
It may have been moved, edited, or deleted.
ERR_FILE_NOT_FOUND ".
The html file is in the specific PyCharm directory, near the main.py file.
You redirect('/') after processing the POST data. But / is not a route that you defined in your flask app. Therefore when your browser follows that redirect I assume it shows the ERR_FILE_NOT_FOUND error, which is a simple 404 error page generated by it (Chrome?).
That's not an error on the Python/Flask server though. There in the stdout print you should see the email value just fine.
If you assumed to see the email in the browser instead, you should have done something like
def signup():
email = request.form['email']
return email # DEBUG return
Related
This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 1 year ago.
I am trying to learn flask.
My login.html file-
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<table>
<tr><td>Name</td>
<td><input type ="text" name ="uname"></td></tr>
<tr><td>Password</td>
<td><input type ="password" name ="pass"></td></tr>
<tr><td><input type = "submit"></td></tr>
</table>
</form>
</body>
</html>
And my main.py file has this-
#app.route('/login',methods = ['POST'])
def login():
uname=request.form['uname']
passwrd=request.form['pass']
if uname=="ayush" and passwrd=="google":
return "Welcome %s" %uname
I am not able to understand how is this able to access login.html without specifying. Also also please explain what is the code in main.py means.
You have to specify the 'html' in flask to access it, however, if you open the html file in browser this will still work since its action is aimed directly at your flask server.
the code of your main.py says that if the in the form sent the data 'uname' and 'pass' are respectively 'ayush' and 'google', the code sends back to the browser a text indicating: "Welcome ayush"
If you want to directly implement the html in your flask web server, you have to create the function and put your html code in templates folder.
from flask import render_template
...
#app.route('/', methods=['GET'])
def code():
return render_template('index.html', name='')
So you can access with http://localhost:5000/ now
I'm trying to get a simple web form up and running that only asks for a URL.
This is the HTML Code (index.html)
<!DOCTYPE html>
<html>
<body>
<form name = 'test' action = "." method = "post">
<form action="test.php" method="get">
URL <input type="text" link="link" name = "URL"/>
<input type="submit" />
</form>
</body>
</html>
I'm using Flask to run the simple web application this is the Flask Code: (app.py)
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route("/")
def index():
return render_template('index.html')
#app.route("/", methods = ["POST"])
def get_value():
url = request.form["URL"]
return 'The url is ' + url
if __name__ == "__main__":
app.run(debug=True)
and I'm trying to get the inputted URL to another python script so I can do something with it, this is the other python script: (url.py)
from app import get_value
print(get_value())
However, whenever I run python3 url.py it gives me this error:
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
Any idea how to print get the URL over successfully? In a lot of detail preferably because I am very new to Flask.
The error occurs because you called a function that needs data from a request to get the user inputs. You should call the url handling function instead letting the handling function call the retrieval of the url.
Consider this answer https://stackoverflow.com/a/11566296/5368402 to make sure you pass the url correctly. Now that you have your url, simply pass it to your other script.
import url # your url.py module
#app.route("/", methods = ["POST"])
def get_value():
input_url = request.form["URL"]
url.handle_url(input_url) #call a function inside url.py
I am trying to create a web page using python and flask on pythonanywhere.com
The page is very simple. The user will enter a url in the box and click submit, the page then shows the url they submitted.
I am trying to do it on one page. If the method is GET, then display the form and allow the user to submit a url. If the method is POST, then print the url passed in the form.
I tried it multiple ways but it did not work. I can see the form and submit the url, but could never print it or put every thing in one page
from flask import Flask, request, render_template
app = Flask(__name__)
#app.route("/", methods = ['GET', 'POST'])
def index():
return '''
<form action="process" method="post">
<p>Enter a URL for a\ web page:</p>
<input type="text" name="url"></input>
<input type="submit" value="Process"></input>
</form>'''
#app.route("/process", methods = ['GET', 'POST'])
def process():
url = request.form['url']
print("The url address is '" + url + "'")
When you print, this would go into your PythonAnywhere webapp logs. Instead, if you want to get that back as a website response, you would have to return it.
(and also reload your webapp after you make any changes)
I apologize for any misinformation from the title but I'm really not sure what the issue is. I'm creating a demo project that receives a user's name and age from an HTML form. Then, there are two buttons. One adds the information to an sqlite3 database called people.db. The other retrieves one person randomly from the database and displays it.
Here is my code:
import os
import sqlite3
from flask import Flask
from flask import request
from flask import render_template
from flask import g
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def hello():
error = None
if request.form.get('submit', None) == "add":
if request.form['name'] and request.form['age']:
name = request.form['name']
age = request.form['age']
database = connect_db()
cursor = database.cursor()
sql = "INSERT INTO person (name, age) VALUES ({0}, {1});".format(name, age)
cursor.execute(sql)
database.commit()
return render_template("index.html")
else:
error = "Name or age not provided."
return render_template('index.html', error=error)
elif request.form.get('submit', None) == "retrieve":
database = connect_db()
cursor = database.cursor()
sql = "SELECT * FROM person ORDER BY RANDOM() LIMIT 1;"
cursor.execute(sql)
result = cursor.fetchone()
return render_template("index.html")
return render_template("index.html")
if __name__ == "__main__":
app.run(host=os.getenv('IP', '0.0.0.0'),port=int(os.getenv('PORT', 8080)))
So, the issue is that when I run the program on Cloud 9's c9users.io platform, it attempts to go to http://project-username.c9users.io:8080/localhost/?name=name&age=22&submit=add. I don't understand why it's trying to access localhost here. My program's structure is as follows:
\website-test
\templates
index.html
hello.py
people.db
So, I suppose it should be returning to the root of the website while performing the desired functionality. How do I achieve that?
Thank you!
Here, also, is my index.html:
<!DOCTYPE html>
<html>
<head>
<title>WEBSITES</title>
</head>
<body>
<h2>Please enter your name and age.</h2>
<form action="localhost/">
<input type="text" name="name" placeholder="name">
<input type="text" name="age" placeholder="age">
<input type="submit" name="submit" value="add">
<input type="submit" name="submit" value="retrieve">
</form>
</body>
</html>
By default Flask run your application on localhost. Add below lines of code to end of the file.
if __name__ == "__main__":
app.run(host='0.0.0.0')
0.0.0.0 here means, the app will take the host as your ip and will be accessible publically. For further reference read here in documentation.
What is in your index.html? Sounds like you have action="localhost/" on the form element
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('/')
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.
try action = "{{url_for('signup'}}"
also when you use print in flask it's seen on the console not the webpage