where some examples don't work
Pages 296-300 of that book
Task 4: Create Code to Work with Our Webapp’s Database and Tables
Determine the Flask version.
First way: two lines in IDLE
import flask
print(flask.__version__)
Second way: one command in Command line:
pip show flask
The installed Flask version is uptodate - 3.0.3
Determine the Python version.
First way: two lines in IDLE
from platform import python_version
python_version()
Second way: one command in Command line again:
python -V
The installed Python version is uptodate too - 3.12.3
Coding in Python, Flask
from flask import Flask
from flask import render_template, request
The method request.user_agent.browser
returns nothing.The next method requires one more importing
from ua_parser import user_agent_parser
These three lines returns the browser name in browser variable:
web_detail = str(request.user_agent)
user_browser = user_agent_parser.ParseUserAgent(web_detail)
browser = user_browser['family']
or briefer in single code line
user_agent_parser.Parse(request.user_agent.string)['user_agent']['family']
I digress a little from the main trend of this note and give a method that receives all the User Agent:
request.headers.get("user-agent")
Summing up, at the beginning, I believed the problem was that "request.user_agent.browser" did not return anything and found other methods to get the browser name. But still nothing was written to the database. I couldn't write to the field "browser_string" in the table "log" of the database "vsearchlogDB" anything, the value after insert operation was Null