I am not able to get the http://127.0.0.1:5000/user1 Url from the browser every time it gives 404 and for the http://127.0.0.1:5000 it gives old output which I have tried (some other string "Hello flask") instead of "hello world"
I have tried closing my IDE and trying again even I have created another project but the problem still persist
Thanks in advance!
#imports
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'hello world!'
#add users to collection "app_users"
#app.route('/user1',methods=['POST'])
def add_user():
if not request.json or not 'email_id' in request.json:
abort(404)
print("processing")
insert(request)
return flask.jsonify({'status': "inserted successfully"}), 201
if __name__ == '__main__':
app.run()
Are you restarting the server when reloading #app.route('/')? Also, try clearing your cache. With Chrome, you can do that by hitting SHIFT + F5.
#app.route('/user1', methods=['POST']) moves to abort since you are not posting anything in your view. What exactly are you trying to do in this route?
Also, while developing, I recommend running app run with the debug parameter:
app.run(debug=True)
Try specifying the port in app.run()
app.run(debug=True,
host='0.0.0.0',
port=9000,
threaded=True)
I too faced the same issue. Sometimes there might installation issues I guess.
In my case I just pip uninstalled flask and reinstalled and it worked fine ;)
To pip uninstall use :
pip uninstall flask
and check whether the servers aren't working, it obviously shouldn't then reinstall by
pip install flask
;)
Are you submitting a form with the parameter (email_id) you set? Also, make sure you are submitting the form with Content-Type: application/json - otherwise, request.json will be empty. You can also use request.get_json(force=True), but this is not recommended. (See How to get POSTed json in Flask?)
Related
I tried to find a solution for my problem in other questions but I couldn't.
I downloaded the python flask and made my first flask app and it ran fine.
Here is the code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
When I ran my second file where I had added an app.route ("/ david") and followed the same procedure again, refreshed it and nothing changed.
That is to say, I was going to / david and I get an URL error
Here is my second file
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
#app.route("/david")
def david():
return "Hello, David!"
I tried the same with other files which have some added routes and the result is the same as the first file
Thanks for your answers, I hope to solve my problem.
You did not run the app. What you did is just create a structure for flask, but did not start the server.
Just add:
app.run()
To the bottom of the file and it will work. It will with start the flask server at http://localhost:5000.
By default, flask runs on port 5000.
It can be changed by:
app.run(host="0.0.0.0", port=xxxx)
0.0.0.0 means it accepts request from anywhere on the port specified.
Make sure you have all the permissions and nothing else is running if you want it to run on port 80.
Hope this helps. Good luck.
I had the same issue. Try first by restarting your IDE; this worked for me. If that doesn't work, try clearing your ports for Windows:
Open Task manager
Click on the “Processe” tab
Enable the "PID" column: View -> Select Columns -> Check the box for PID
Find the PID (in your case, 5000 - flask default port) and click “END PROCESS"
Hey I ran the flask basic code as follows -
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
Then I ran the server as stated in docs and it ran fine.
But now when i got 127.0.0.1:5000/ nothing happens. The browser keeps circling as if refreshing the page but doesn't route.
Its my first python/flask code so I am not sure what I am doing wrong.
EDIT- By docs I mean quickstart documentation of flask. I know its fine cz i get this -
Serving Flask app "hello"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Just added app.run() to the same code and executed which is working fine. Can you try it?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
app.run()
I'm assuming that you're using this Quickstart - A Minimal Application.
That said let's make some points clear:
When you said
I know its fine cz i get this - Serving Flask app "hello"
You do not use the extension .py, which can cause some problems if you have another file with the same name in this directory. So make sure your FLASK_APP variable is correct.
This should work for you, but if the problem persist enable the Debug Mode adding
FLASK_ENV=development to your environment variable and see which error appears for you.
Hoping this solve your problem.
I am a newbie in the flask development this is my first program in the flask but it shows me this error:
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
& this is my code
from flask import Flask
app = Flask(__name__)
#app.route('/index')
def index():
return 'Hello World'
if __name__ == '__main__':
app.run(debug=True)
I think you should just go to http://localhost:5000/index or http://127.0.0.1:5000/index but if you want to make that page your code should be like that
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__main__':
app.run(debug=True)
change #app.route('/index') to #app.route('/') also you should check this http://flask.pocoo.org/docs/0.12/quickstart/#routing
You have to specify the route for index page as
#app.route('/')
If you give any other name to the template, you need to have to specify the name to the route.
For eg, if the template name is "home", then you have to give it as:
#app.route('/home')
I came across this question while having an almost similar problem. But in my case, the app.py would be executed, for the first time, then if i try to reload my localhost, maybe numerous times after making changes on my app.py the above cited error would be generated.
To solve this, i got a solution from this link: https://stackoverflow.com/a/44950213
Basically the last execution of python would continue running even after i updated my files and my console indicated a restart. So lets say you have made 3 changes while saving each change, then in the background you will have 3 instances of python.exe running. Depending on your OS, you will need to end these processes and re-execute your app.py.
So even if you use http://127.0.0.1:5000/index yet there are still instances of previous python.exe running, it may not execute
Note: this doesnt have to be the case all the time.
if you make the correct changes and it still doesn't work, I found out the hard way that the answer is to just save your file, and then run the program again.
I had the same problem. I created the home page which ran just fine:
#app.route('/')
def hello_world():
return 'Hello, World!'
But then when I tried making another page the next day:
#app.route('/bye/')
def bye():
return 'Bye!'
It gave me a 404 error.
So this is what I did to solve it:
Go to terminal ---> set FLASK_APP=youPythonfilename.py ------> flask run
After doing this the problem was solved. To avoid reloading everytime you need to set your debugger to ON.
if __name__ == '__name__':
app.run(debug=True)
After you have turned debugger ON by the above code, all you need to do is save the file everytime you make changes (windows - ctrl+s).
I copy pasted the flask's 'hello world' app from their website and am trying to run it. I get an error message in Chrome saying
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Here is the 'hello world' app straight from flasks website
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run()
What I have tried:
-temporarily disabling Avast!
-disabling windows firewall
-ensuring that the flask module is installed
This was working a couple days ago actually...
I don't know why but when I change
app.run()
to
app.run(port=4996)
it starts working. No idea why the default port is throwing an error. Oh well.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__name__':
app.run()
app.run(port=5000)
For Windows machines you can use the command in cmd:
set FLASK_APP=python_file.py
flask run
Some other process is running on port 5000. It may be you still have an old Flask process running, with broken code. Or a different web server altogether is running on that port. Shut down that process, or run on a different port.
You can switch to using a different port with the port argument to app.run():
app.run(port=8080)
If you can't figure out what process is still bound to port 5000, use the Windows Resource Monitor or run netstat -a -b from a command line. See How can you find out which process is listening on a port on Windows?
I think you are trying to copy the route generated through your flask program in cmd by pressing ctrl+c which quits your running flask program . i was also doing the same.just try to type the route generated by your flask program on your browser . it will definitely resolve your problem.
Where your python file store is, use cmd and then go on your file store directory, then
set FLASK_APP=filename.py
After this your flask run cmd will work.
from flask import Flask
app = Flask(__name__) # creating app
#app.route('/', methods['GET']) #routing it to the home page
def home(): #function
return "hello world"
app.run(port=5000, debug=true) #function call by the app
Add port and use methods whatever your need is USE GET in your case and try to remove your cache and run the this code it will definitely work.
I am a newbie to Python attempting to experiment with sample code under Windows 8.1.
On http://flask.pocoo.org/docs/0.10/quickstart/ it says "if you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger".
I have added to the code app.run(debug=True) to the sample code on the above page. The server will now reload itself on code changes (as promised) but when I create a syntax error the "helpful debugger" does not show. Instead I get an error message in my command prompt.
Any ideas why? I suspect the answer might be here Can't enable debug mode in Flask but it is largely uninteligible to me.
So far I have tried restarting my machine and putting the code in different locations. I am not in a forked environment (as best as I know). For those that are curious my source code is shown below:
from flask import Flask
app = Flask(__name__)
from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
#app.route('/')
def hello_world():
return 'Hello World! #note the deliberate syntax error
if __name__ == '__main__':
app.debug = True
app.run()
The debugger is for debugging exceptions in a syntactically valid program. Try raising an exception in your code, visit the URL in a browser window, and you will see the very helpful debugger. See also http://flask.pocoo.org/snippets/21/
Syntax errors will be shown on the console as you have seen. That's how it works.
For Windows users
The page and resource that was selected as the answer in this post led to a broken page. However, try the code below.
Set debug mode using
$env:FLASK_ENV = "development"
Run app
flask run
This post solved it for me
Or
Set it directly from the app in your code.
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=3000)