giving error the request url was not found on your server - python

i am getting error 404 while running this
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. how do i need to run the server ? please help
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__, template_folder="templates/")
app.route("/login", method=['POST', 'GET'])
def index():
greeting = "Hello World"
if request.method == "POST":
name = request.form['name']
greet = request.form['greet']
greeting = f"{greet}, {name}"
return render_template("index.html", greeting=greeting)
else:`enter code here`
return render_template("hello_form.html")
if __name__ == "__main__":
app.run(debug=True)

You need to replace
app.route("/login", method=['POST', 'GET'])
By
#app.route("/login", methods=['POST', 'GET'])
Edit: also, methods

Related

RuntimeError Error in Flask python on Post

my code:
from flask import Flask, render_template,request, redirect
#app.route("/wait", methods=['GET', 'POST'])
def wait():
print(5)
return render_template("wait")
#app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
data = request.form
print(data["theme"])
print(data["subreddit"])
return redirect("/wait")
else:
return render_template("index.html")
my error:
RuntimeError: Attempt to access app outside of a relevant context
You need to instantiate app in your code. Add this line under your import:
app = Flask(__name__)

Flask method PUT not returning something

Hello gus so I have this code:
import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/set', methods=['POST'])
def set():
auth = request.args.get('auth')
username = request.args.get('username')
password = request.args.get('password')
return auth
app.run()
What I want to do is that I want to be able to work with those variables but I wanted to see if I can get them at first, I have runned the script and entered the link:
http://127.0.0.1:5000/set?auth=hello&username=adi&password=123
I get the "Method Not Allowed" when I run that and no output as it should return the auth value
You didn't allow get method:
#app.route('/set', methods=['POST', 'GET'])

Flask routing not working

It's the most basic thing to ask but #app.route('/') is not working on my Linux server.
Below is the code :
from flask import Flask, jsonify, request
from app import models
import json
import time
app = Flask(__name__)
app.url_map.strict_slashes = True
#app.route('/')
def blank():
return 'Hello ABC!'
#app.route('/driftking')
def blank2():
return 'Hello driftking!'
# dynamic route
#app.route("/test/<search_query>")
def search(search_query):
return search_query
#app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return "POST METHOD"
elif request.method == 'GET':
return "GET REQUEST ARE NOT ALLOWED"
if __name__ == '__main__':
app.run(debug=True)
app.run()
Very basic app, all's working fine on local machine but not on my linux server.
E.g. if I load http://xxx.xxx.xxx.xxx/projectname ---- it shows Hello ABC!
If I load http://xxx.xxx.xxx.xxx/projectname/driftking -- it redirects me to http://xxx.xxx.xxx.xxx ( i.e my server's homepage)
If I load http://xxx.xxx.xxx.xxx/projectname/test/search -- 404 error not found
If I load http://xxx.xxx.xxx.xxx/projectname/login -- it redirects me to http://xxx.xxx.xxx.xxx ( i.e my server's homepage)
127.0.0.1 - - [24/Nov/2017 19:37:01] "POST //login HTTP/1.1" 405 -
^^This is what I get on terminal. I don't understand why I get two leading slashes everytime.
But If do http://xxx.xxx.xxx.xxx/projectname/insert-any-word/login , my post req get's executed. At the same time on local machine i dont get two leading slashes // to the path and thus the request get's processed.
There are a couple of things wrong but try this, you'll need a proxy to help with the rerouting.
from flask import Flask, jsonify, request
from app import models
import json
import time
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.url_map.strict_slashes = False
#app.route('/')
def blank():
return 'Hello ABC!'
#app.route('/driftking')
def blank2():
return 'Hello driftking!'
# dynamic route
#app.route("/test/<search_query>")
def search(search_query):
return search_query
#app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return "POST METHOD"
elif request.method == 'GET':
return "GET REQUEST ARE NOT ALLOWED"
if __name__ == '__main__':
app.run(debug=True)

Flask Print Not Working

If I send run this program to do a HTTP Post to my Flask server, which I know returns a 200 response:
import requests
import json
dump= '{"on": false}'
r = requests.post('http://127.0.0.1:5000', data=dump,
headers={'Content-Type': 'application/json'})
And my Flask server's code:
from flask import Flask
from flask import request, jsonify
import requests
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def signal():
if request.method == 'POST':
content = request.get_json()
return jsonify(content)
print(jsonify(content))
r = requests.put("http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state/", jsonify(content))
else:
return 'Hello, world!'
if __name__ == '__main__':
app.run(debug=True)
I want to print the data to the console, then send it over to a bridge on the network using a HTTP PUT. Neither of these are working, and I'm not sure why.
You need to return at the very end of the function
#app.route('/', methods=['GET', 'POST'])
def signal():
if request.method == 'POST':
content = request.get_json()
print(content)
r = requests.put("http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state/", content)
return jsonify(content)
else:
return 'Hello, world!'
Note: You probably are over-using the jsonify function because the jsonify() function in flask returns flask.Response() object, and not a JSON string that you would POST or PUT to another service.

How to print output using only a POST Method?

How can I print something like this:
{
username = admin
email = admin#localhost
id=42
}
With only using a method = ['POST'] and without using render_template?
PS: I already made it run with ['GET']
Here's my code:
from flask import Flask, jsonify, request
app = Flask(__name__)
#app.route('/', methods=['POST'])
def index():
if request.method == 'POST':
return jsonify(username="admin",
email="admin#localhost",
id="42")
else:
if request.method == 'POST':
return jsonify(username="admin",
email="admin#localhost",
id="42")
if __name__ == "__main__":
app.run()
And what I get is a 405 Method error.
Hey make sure your trailing stashes in your html are correct.
you may refer to : Flask - POST Error 405 Method Not Allowed and flask documentation : http://flask.pocoo.org/docs/0.10/quickstart/
this
<form action="/" method="post">
and this is same same but different
<form action="" method="post">
Accessing it without a trailing slash will cause Flask to redirect to the canonical URL with the trailing slash.
Given your error 405, I am suspecting that this is your problem. GET is fine, because you will just be redirected.
Try returning the form (as biobirdman said) on a GET request. Not sure why you need the request.method == 'POST' conditional statement. The parameter methods=['POST'] in the route should suffice.
Try this:
from flask import Flask, jsonify, request
app = Flask(__name__)
#app.route('/', methods=['POST'])
def index():
return jsonify(username="admin", email="admin#localhost", id="42")
#app.route('/', methods=['GET'])
def form():
return "<form action='/' method='POST'>" \
"<input type='submit'>" \
"</form>"
if __name__ == "__main__":
app.run()

Categories