Google app engine form not working - python

I'm having troubles with Google App Engine. It is all set up (and working hour before), when I reconnected the Engine the form simply stop working, when I hit submit, the page goes to the server's page and nothing happens.
<!DOCTYPE html>
<html lang="en">
<body>
<form action="http://localhost:9080/sub" method="post">
<input type="email" name="email" placeholder="Email"/>
<input type="submit" value="Subscribe"/>
</form>
</body>
</html>
import webapp2
class SubscribeHandler(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
email = self.request.get("email")
if email == '':
self.response.out.write("no email")
self.response.out.write(email)
app = webapp2.WSGIApplication([('/sub', SubscribeHandler)], debug=True)

Related

POST method is not working on Flask application - Error 404

I am trying to build a simple Flask application that will run a web app that connects to a Postgres database.
However, when I run the code and click the submit button, the POST method does not work, so it does return the {{url_for('success]}} as specified. I have tried adding and removing several components.
I have methods=['GET', 'POST'] already.
app.py:
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:postgres#localhost/Jeopardy'
db=SQLAlchemy(app)
class Data(db.Model):
__tablename__="allc"
number = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String())
question = db.Column(db.String())
answer = db.Column(db.String())
def __init__(self,number,category,question,answer):
self.number = number
self.category = category
self.question = question
self.answer = answer
#app.route("/")
def index():
return render_template("index.html")
#app.route("/success", methods=['POST','GET'])
def success():
if request.method=='POST':
category=request.form['category']
question=request.form['question']
answer=request.form['answer']
number=request.form['number']
print(email, height)
if db.session.query(Data).filter(Data.number == number).count()== 0:
data=Data(number,category,question,answer)
db.session.add(data)
db.session.commit()
return render_template("success.html")
if __name__ == '__main__':
app.debug=True
app.run()
index.html:
<html lang="en">
<title>Jeopardy</title>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device=width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="../static/style.css" rel="stylesheet" >
</head>
<body>
<div class="="container">
<img src="../static/logo.png" alt="Jeopardy" class ="logo">
<!-- #todo - message-->
<form action = "s{{url_for('success')}}" methods="POST">
<div class = " form-group">
<h3>Jeopardy Question</h3>
<input
type = "number"
name = "Index"
placeholder= "Type number" />
<input
type = "text"
name = "Question"
placeholder= "Type the Jeopardy question here" />
<input
type = "text"
name = "Answer"
placeholder= "Type the Jeopardy Answer here"/>
<button type = "submit"> Submit</button>
</form>
</div>
</body>
</html>
While running the code my app renders successfully, but when submitting a number the server does not register the input. When loading the success page separately, it loads.
In the terminal, I see: "POST /success HTTP/1.1" 404 -
You have a typo in your html
It should be method="post" and not methods="post"
EDIT:
Another typo in
action = "s{{url_for('success')}}"
Remove the "s"
here is typo:
<form action = "s{{url_for('success')}}" methods="POST">
change it to:
<form action = "{{url_for('success')}}" method="POST">

Connect HTML page with Elasticsearch using Python flask

I am new to web development. I am trying to create a web page which will display index from elastic search database. I am using python flask for backend.
I see html page and python console shows index.
But I am not able to fetch index from HTML page.
I am not sure what could be the issue
Python code is as follows:
from flask import Flask,render_template, request
from elasticsearch import Elasticsearch
app = Flask(__name__)
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
doc1 = {"food": "Japanese", "spice_level": "moderate"}
doc2 = {"food": "Italian", "spice_level": "mild"}
doc3 = {"food": "Indian", "spice_level": "spicy"}
es.index(index="food", doc_type="spice_level", id=1, body=doc2)
resp = es.get(index="food", doc_type="spice_level", id=1)
print(resp)
#app.route('/')
def home():
return render_template('index.html')
app.route('/dashboard', methods=['GET', 'POST'])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
HTML code is as follows:
<!DOCTYPE html>
<BODY bgcolor="cyan">
<form method="GET" action="/dashboard">
<center>
<H1>Database UI </H1> <br>
search here <input type = "text" name= "index" /> <br>
<input type = "submit">
</center>
</form>
</BODY>
</html>
Whenever I type a index name and click on search button, page gives me error as :
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
I cannot see any other error then this, and it's really difficult to debug with less information about error.
why your /dashboard return 404 ?
because lack of view function to response.
app.route('/dashboard', methods=['GET', 'POST']) is invalid.
How to access /dashboard of elascticsearch ?
In your case, the simplest way is modify the index.html
<!DOCTYPE html>
<BODY bgcolor="cyan">
<form method="POST" action="http://localhost:9200/dashboard">
<center>
<H1>Database UI </H1> <br>
search here <input type = "text" name= "index" /> <br>
<input type = "submit">
</center>
</form>
</BODY>
</html>
can you use this here? for Parse data from html to python code you need to have POST inside #app.route like this:
#app.route("/", methods=['GET', 'POST'])
def home():
return render_template('index.html')
if you want to pase data into index.html you can use this here:
somedata = "variable string"
render_template('index.html', somedata=somedata)
inside index.html do {{ somedata }}
<!DOCTYPE html>
<BODY bgcolor="cyan">
<form method="POST" action="">
<center>
<H1>Database UI </H1> <br>
<!-- it will show (variable string) -->
{{ somedata }}
search here <input type = "text" name= "index" /> <br>
<input type = "submit">
</center>
</form>
</BODY>
</html>
happy codeing.

Basic flask form handling

I am trying to create a basic form in Flask that will take in the input, manipulate it and then return the output. I am running into the issue that when I am running the terminal and trying to make my app work on the http://127.0.0.1:5000/ server, the files are not visible. Not sure where the bug is?
This is how I have my files organized:
/Users/eas/Desktop/grota/templates/index.html
/Users/eas/Desktop/grota/templates/age.html
/Users/eas/Desktop/grota/app.py
This is the app.py file
from flask import Flask, render_template,request
app = Flask(__name__)
#app.route('/send',methods = ['GET','POST'])
def send():
if request.method == 'POST':
age = request.form['age']
return render_template('age.html',age=age)
return render_template('index.html')
if __name__ == '__main__':
app.run()
This is the index.html file
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<h1>How old are you?</h1>
<form method="POST" action = "/send">
<div class = "form-group">
<input type="text" name = "age">
</div>
<input class="btn btn-primary" type="submit" value="submit">
</form>
</body>
</html>
This is the age.html file
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Your age is{{age}}</h1>
</body>
While looking for a problem, which I try to solve I found your question. For a solution that includes full code (with py and 2 HTML files) you can see my repl.it : https://repl.it/#AlperenAtik/NaturalShortActivecell#main.py
The problems are:
In the first function, you routed your loop to "/send" page. This is why your send page did not appear. The route of the first function needs to be "/"
In the index file, the source route was shown as '/send'. Whenever I changed it to / the script worked without a problem
The other commentators already mentioned your triggering function has an indentation mistake. When host and port added appropriately, your script would work.
for ones who love the harder way- seeing things in stackoverflow- not in action, I am adding the codeblocks. Enjoy.
for main.py
from flask import Flask, render_template,request
app = Flask(__name__)
#app.route('/',methods = ['GET','POST'])
def send():
if request.method == 'POST':
age = request.form['age']
return render_template('age.html',age=age)
return render_template('index.html')
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 8080
I left the host as '0.0.0.0' and port as 8080. If you are running Python on a terminal, you may try (host = '127.0.0.1', port:5000').
for templates/index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<h1>How old are you?</h1>
<form method="POST" action = "/">
<div class = "form-group">
<input type="text" name = "age">
</div>
<input class="btn btn-primary" type="submit" value="submit">
</form>
</body>
</html>
for templates/age.html
<html>
<head>
</head>
<body>
<h1>Your age is {{age}}</h1>
</body>
You did not set the root route.
Or you open http://127.0.0.1:5000/send
Or you could use this quick and dirty fix (as you see in decorators now both / and /send are taken into account):
from flask import Flask, render_template,request
app = Flask(__name__)
#app.route('/',methods = ['GET'])
#app.route('/send',methods = ['GET','POST'])
def send():
if request.method == 'POST':
age = request.form['age']
return render_template('age.html',age=age)
return render_template('index.html')
if __name__ == '__main__':
app.run()
If you do not handle the '/' route nothing will happen when you open http://127.0.0.1
If you explain more clearly what is the result that you want to obtain I can help better.
Try going here: http://127.0.0.1:5000/send
If that doesn't work, what error are you getting in either your console or in your browser?
EDIT:
I just tried it, and it worked. Try reloading the page in a new tab in your browser, and see if it still occurs. This error has to do with the naming of your inputs on the webpage and the way you index your form with request.form['age']

Flask-Sqlite posting to DB from webform

I have started just learning Flask and tried to find an answer how to post to SQLite DB from webform. So far haven't managed to get it work and bit lost with this. I manage to print values from DB based on code sample from simplypython but don't know how to add new ones from webform.
I would need to be able to address elements, open connection to the database, Insert values, save and close the connection. As far as I am aware I should add the POST method to the app.py and use request.form statement to pull the elements when submit button is pressed.
then the code should automatically display all the values on index html, which already works. could you please help me with the code I need to add to app.py file to get values added to DB and what to add to form action webform-section on html file?
index.html
<!DOCTYPE html>
<html>
<head>
<title>Flask Intro</title>
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
</head>
<body>
<div class="container">
<h3 potsit </h3>
{% for post in posts %}
Titleotsikko: {{post.title }} <br>
Postotsikko: {{post.description}}
{% endfor %}
</div>
<div>
<form action="/????????????NOT SURE WHERE TO DIRECT" method="post">
<div>
<label for="title">title:</label>
<input type="text" id="title" />
</div>
<div>
<label for="description">description:</label>
<input type="text" id="description" />
</div>
<div class="button">
<button type="submit">Add to db</button>
</div>
</form>
</div>
</body>
</html>
app.py
from flask import Flask, render_template, request, session, g
import sqlite3
app = Flask(__name__)
#app.route('/')
def home():
g.db = sqlite3.connect("sample.db")
cur = g.db.execute('select * from posts')
posts = [dict(title=row[0], description=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template("index.html", posts=posts)
if __name__=='__main__':
app.run(debug=True)
sql.py
import sqlite3
with sqlite3.connect("sample.db") as connection:
c = connection.cursor()
c.execute("DROP TABLE posts")
c.execute("CREATE TABLE posts(title TEXT, description TEXT)")
c.execute('INSERT INTO posts VALUES("Luck", "no luck.")')
EDIT
I made some changes based on the suggestion from Paul Rooney and created file called post.html, moved the form there from index.htmland added another #route on the app.py file. I believe I'm not far off but after trying to figure this out since Tuesday hoping to get it work. Unfortunately now I receive an error 405 'The method is not allowed for the requested URL.' and I'm stuck with this after trying different options.
post.html
<!DOCTYPE html>
<html>
<head>
<title>Flask post</title>
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
</head>
<body
<div>
<form action="/post" method="post">
<div>
<label for="title">title:</label>
<input type="text" id="title" />
</div>
<div>
<label for="description">description:</label>
<input type="text" id="description" />
</div>
<div class="button">
<button type="submit">Add to db</button>
</div>
</form>
</div>
</body>
</html>
(edited) app.py
from flask import Flask, render_template, request, session, g, redirect, url_for
import sqlite3
app = Flask(__name__)
#app.route('/')
def home():
g.db = sqlite3.connect("sample.db")
cur = g.db.execute('select * from posts')
posts = [dict(title=row[0], description=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template("index.html", posts=posts)
#app.route('/post', methods=['POST'])
def post():
title=request.form['title']
description=request.form['description']
print title, description
return redirect(url_for('/'))
if __name__=='__main__':
app.run(debug=True)
To answer your original question you should pick an endpoint to post your data to and use that in both your flask app and the html form action.
I suggested post in comments but it could be anything. I think you've grasped what to do here but for completeness.
The HTML
<form action="/post" method="post">
The Python Code
Add a route to handle the POSTed data.
#app.route('/post', methods=['POST'])
def post():
# Do db stuff then redirect back to index page.
pass
Edited Question
You have an indentation error in your code, which is causing the 405 error. Your post function is inside your home function. Make it more like this
app.py
from flask import Flask, render_template, request, session, g, redirect, url_for
import sqlite3
app = Flask(__name__)
#app.route('/')
def home():
g.db = sqlite3.connect("sample.db")
cur = g.db.execute('select * from posts')
posts = [dict(title=row[0], description=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template("index.html", posts=posts)
# One level of indentation removed from here downwards
#app.route('/post', methods=['POST'])
def post():
title=request.form['title']
description=request.form['description']
return redirect(url_for('home'))
if __name__=='__main__':
app.run(debug=True)
After that I see a 400 Bad Request error, which stems from the fact that you don't have name parameters in your html form.
The attempt to access the title and description values in the form dict in app.py will throw a KeyError exception, as those keys will not be present without the name parameter in the html.
If you add them e.g.
...
<input type="text" id="title" name='title'/>
...
<input type="text" id="description" name='description'/>
...
Then it will run all the way through your function.
The next issue will be
redirect(url_for('/'))
instead use
redirect(url_for('home'))
home being the name of the function called for path '/'.
After that you should be good to go.

Why does request.get() return empty string?

I am new to python and google app-engine. I made the simple code to get input from a form and then display it on a new page . However, the self.request.get() methods returned empty strings. Why is this happening and how do I solve this problem?
import os
import jinja2
import webapp2
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)
class NewPost(webapp2.RequestHandler):
def get(self):
self.response.out.write(jinja_env.get_template('newpost.html').render())
def post(self):
title = self.request.get('title')
body = self.request.get('body')
self.redirect('/message')
class Message(webapp2.RequestHandler):
def get(self):
title = self.request.get('title')
body = self.request.get('body')
self.response.out.write('message: ' + title + body)
app = webapp2.WSGIApplication([webapp2.Route(r'/', handler=NewPost, name='newpost'), webapp2.Route(r'/message', handler=Message, name='message')], debug = True)
the newpost.html is:
<!DOCTYPE HTML>
<html>
<head>
<title> Title </title>
</head>
<body>
<form method="post">
<label>
<div>Title</div>
</label>
<input type="text" name="title" value="{{title}}">
<label>
<div>Body</div>
</label>
<textarea name="body">{{body}}</textarea>
<input type="submit">
</form>
</body>
</html>
The form parameters are available to the POST / request (NewPost.post()), but they are not carried forward on the redirect to /message. You have to store the form data somehow (such as to the datastore) when handling the POST, then retrieve it from storage after the redirect.

Categories