I'm learning web development in python with the flask. The code doesn't show any error, but when I run it, the browser shows Error - 404 Not Found.
Code:
from flask import Flask, redirect, url_for
app = Flask(__name__)
#app.route('/admin/')
def hello_admin():
return 'Hello Admin'
#app.route('/guest/<guest>')
def hello_guest(guest):
return 'Hello %s as Guest' % guest
#app.route('/user/<name>')
def hello_user(name):
if name =='admin':
return redirect(url_for('hello_admin'))
else:
return redirect(url_for('hello_guest',guest = name))
if __name__ == '__main__':
app.run(debug=True)
Output in Chrome:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again
From Flask documentation:
An important detail to keep in mind is how Flask deals with trailing slashes. The idea is to keep each URL unique so the following rules apply:
If a rule ends with a slash and is requested without a slash by the user, the user is automatically redirected to the same page with a trailing slash attached.
If a rule does not end with a trailing slash and the user requests the page with a trailing slash, a 404 not found is raised
IMO, you may encounter the second situation.
Related
I don't understand why I am getting a KeyError. When running my code through the idle debugger.The directory with the requests module/folder is added to the PATH. Installing with pip shows that all of the dependencies are installed, and the module shows up when I run pip freeze. I'm not sure what the problem is here... Any help would be greatly appreciated.
import requests
def get_btc_price():
# Make the API call to retrieve the current price of Bitcoin
response = requests.get(
"https://api.kucoin.com/api/v1/market/orderbook/level2_100?symbol=BTC-USDT"
)
# Check if the API call was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Return the current price of Bitcoin
return print(float(data["data"]["bids"][0][0]))
# If the API call was unsuccessful, return 0
return 0
get_btc_price
I think there was a conflict with an old version of Python... I deleted all PATH variables related to Python, then did an uninstall/reinstall of Python and the issue is resolved.
So I'm trying to add all auth to existing DjangoCMS project and ran into this kinda weird issue. I added urls like this:
urlpatterns += i18n_patterns(
url(r'^admin/', admin.site.urls), # NOQA
url(r'^', include('cms.urls')),
path('account/', include('allauth.urls')), # <-- here )
But when I go to any url, say mydomain.com/en/account/login/ I get 404. In debug mode Django lists all sorts of patterns it tried to match, and among them - en/ account/ login/ [name='account_login'], concluding with
"The current path, /en/account/login/, didn't match any of these."
Moreover if I add {% url 'account_login' %} to a template, it works just fine, producing a link to mydomain.com/en/account/login/ which is still a 404 error. I have no idea where to even start debugging this :( Any suggestions?
Edit:
the contents of my allauth urls.py https://pastebin.com/PZDH6EDm
Django version 3.0.7
Replace the url with path:
urlpatterns += i18n_patterns[
path(r'^admin/', admin.site.urls), # NOQA
path(r'^', include('cms.urls')),
path('account/', include('allauth.urls')), # <-- here
]
I am building a small server application in Flask as part of a project I am working on. Part of the functionality is the ability to upload a small file of instructions for one of the key injectors to download. Each key injector has a name (''') which corresponds to a client and the file is uploaded to the server via a POST request.
The following listing is the Flask code.
#app.route('/upload/instructions/<ducky_name>/', methods = ['POST'])
def upload_Instruction(ducky_name):
file = request.files()
path = os.getcwd() +"/files/" + ducky_name
with open(path, "w") as f:
f.write(file)
print(f)
f.close()
return "Success"
And I am using this curl command to upload the file.
curl -X POST -d #test http://127.0.0.1:5000/upload/instructions/test1
I then get a 308 redirect, and the file is not uploaded. This is the first time I've dealt with uploading files as a POST in this way, and the also the first time I've used Flask.
Thanks
The URL you use in the curl request does not have the trailing slash as in your Flask route. In this case the framework redirects you to the route with slash (see the documentation entry). So just add the trailing slash:
curl -X POST -d #test http://127.0.0.1:5000/upload/instructions/test1/
Flask uses 308 HTTP response code instead of more common 301 to preserve the request method and body during redirect.
Flask use werkzeug.routing.Rule, which enable strict_slashes as default, visiting a branch URL without a trailing slash will redirect to the URL with a slash appended. Which cause response with 308 (Permanent Redirect).
If you want to support both routes:
/upload/instructions/<ducky_name>/
/upload/instructions/<ducky_name>
Just set app.route with strict_slashes=False, like this:
#app.route('/upload/instructions/<ducky_name>/', methods = ['POST'],
strict_slashes=False)
def upload_Instruction(ducky_name):
pass
refer: https://werkzeug.palletsprojects.com/en/1.0.x/routing/
I'm using python 3.7.3, with flask version 1.0.2.
When running my app.py file without the following imports:
import logging
logging.basicConfig(filename='api.log',level=logging.DEBUG)
Flask will display relevant debug information to console, such as POST/GET requests and which IP they came from.
As soon as DEBUG logging is enabled, I no longer receive this output. I have tried running my application in debug mode:
app.run(host='0.0.0.0', port=80, debug=True)
But this produces the same results. Is there a way to have both console output, and python logging enabled? This might sound like a silly request, but I would like to use the console for demonstration purposes, while having the log file present for troubleshooting.
Found a solution:
import logging
from flask import Flask
app = Flask(__name__)
logger = logging.getLogger('werkzeug') # grabs underlying WSGI logger
handler = logging.FileHandler('test.log') # creates handler for the log file
logger.addHandler(handler) # adds handler to the werkzeug WSGI logger
#app.route("/")
def index():
logger.info("Here's some info")
return "Hello World"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
Other Examples:
# logs to console, and log file
logger.info("Some text for console and log file")
# prints exception, and logs to file
except Exception as ue:
logger.error("Unexpected Error: malformed JSON in POST request, check key/value pair at: ")
logger.error(ue)
Source:
https://docstrings.wordpress.com/2014/04/19/flask-access-log-write-requests-to-file/
If link is broken:
You may be confused because adding a handler to Flask’s app.logger doesn’t catch the output you see in the console like:
127.0.0.1 - - [19/Apr/2014 18:51:26] "GET / HTTP/1.1" 200 -
This is because app.logger is for Flask and that output comes from the underlying WSGI module, Werkzeug.
To access Werkzeug’s logger we must call logging.getLogger() and give it the name Werkzeug uses. This allows us to log requests to an access log using the following:
logger = logging.getLogger('werkzeug')
handler = logging.FileHandler('access.log')
logger.addHandler(handler)
# Also add the handler to Flask's logger for cases
# where Werkzeug isn't used as the underlying WSGI server.
# This wasn't required in my case, but can be uncommented as needed
# app.logger.addHandler(handler)
You can of course add your own formatting and other handlers.
Flask has a built-in logger that can be accessed using app.logger. It is just an instance of the standard library logging.Logger class which means that you are able to use it as you normally would the basic logger. The documentation for it is here.
To get the built-in logger to write to a file, you have to add a logging.FileHandler to the logger. Setting debug=True in app.run, starts the development server, but does not change the log level to debug. As such, you'll need to set the log level to logging.DEBUG manually.
Example:
import logging
from flask import Flask
app = Flask(__name__)
handler = logging.FileHandler("test.log") # Create the file logger
app.logger.addHandler(handler) # Add it to the built-in logger
app.logger.setLevel(logging.DEBUG) # Set the log level to debug
#app.route("/")
def index():
app.logger.error("Something has gone very wrong")
app.logger.warning("You've been warned")
app.logger.info("Here's some info")
app.logger.debug("Meaningless debug information")
return "Hello World"
app.run(host="127.0.0.1", port=8080)
If you then look at the log file, it should have all 4 lines printed out in it and the console will also have the lines.
I am trying to get the code to read in the web page using splash for a more complicated site, but I can't even get the code to run for this simple site location. I ran the docker and have the 8050 port mapped to 0.0.0.0 in my settings.py file. Any help would be greatly appreciated. Please provide version you used for any package as I fear this may be an issue.
I have tried numerous error fixes along the way. Changing the versions of Splash, Scrapy, and Twisted. Scrapy only works on Python 3.x with a newer version of Twisted, but Splash says incomparable with Twisted > 16.2. So I tried switching up the versioning some there with no fixes.
import scrapy
import scrapy_splash
class ExampleSpider(scrapy.Spider):
name = "test"
#allowed_domains = ["Monster.com"]
start_urls = [
'http://quotes.toscrape.com/page/1/'
]
def start_requests(self):
for url in self.start_urls:
yield scrapy_splash.SplashRequest(url, self.parse,
args={
'wait': 0.5,
},
endpoint='render.html',
)
def parse(self, response):
for quote in response.css('div.quote'):
print (quote.css('span.text::text').extract())
I should just receive the Quote Texts, ie. this is the same URL from the python documentation
There is nothing wrong with your code.
Your issue is this:
I have the 8050 port mapped to 0.0.0.0 in my settings.py file
The correct mapping in settings.py should be:
SPLASH_URL = http://localhost:8050
or
SPLASH_URL = http://127.0.0.1:8050