
Here are the requested items generated from the provided content: "Flask URL Routing and Dynamic Content
"Explore Flask's URL rules, dynamic content creation, and canonical URLs. Learn how to make URLs dynamic with variable names and the different types of converters available in Flask for flexible routing."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
from flask import Flask app=Flask(__name__) @app.route('/') def home(): return '<h1>Hello World</h1>' @app.route('/about') def about(): return '<h2>Vivekanand College, Kolhapur<h2>' @app.route('/blog') def blog(): return '<h2>This is the blog of College</h2>'
Variable Rules in Flask It is possible to build URL dynamically by adding variable parts to rule parameters. This variable part is marked as <variable_name> It is passed as a keyword argument to the function with which rule is associated.
How to make URL Dynamic with Variable Name from flask import Flask http://127.0.0.1:5000/ Hello World app=Flask(__name__) @app.route('/') def index(): return 'Hello World' http://127.0.0.1:5000/xyz Hello xyz! @app.route('/<name>') def hello(name): return 'Hello %s!' %name
URL Rules of Flask are based on Werkzeugs Routing Module. This ensures that the URLs formed are unique and based on the precedents laid down by Apache. * Presence and absence of slash ( / ) http://127.0.0.1:5000/flask Hello from flask import Flask app=Flask(__name__) http://127.0.0.1:5000/flask/ Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. @app.route('/flask') def index(): return 'Hello'
http://127.0.0.1:5000/python/ Hello World from flask import Flask app=Flask(__name__) @app.route('/python/') def hello(): return 'Hello World' http://127.0.0.1:5000/python Hello World
Both Rules appear similar, but in the second trailing slash(/) is used. from flask import Flask app=Flask(__name__) As a result it becomes canonical URL. Hence using URL /python or /python/ returns same output. @app.route('/flask') def index(): return 'Hello' However in the flask case when we enter /flask/ it gives error @app.route('/python/') def hello(): return 'Hello World'
Converters available Strings Accepts any character without slash Int Accepts integer Float Accepts Floating point value Path Like default but also accept slash any Mathches any one of the item provided Uuid Accepts uuid strings
from flask import Flask http://127.0.0.1:5000/hello/abc Hello abc app=Flask(__name__) @app.route('/hello/<name>') def hello(name): return 'Hello %s' %name http://127.0.0.1:5000/blog/3 Blog id 3 @app.route('/blog/<int:blogid>') def blog(blogid): return 'Blog id %d' %blogid @app.route('/temp/<float:temperature>') def temp(temperature): return 'Temperature %f' %temperature http://127.0.0.1:5000/temp/4.5 Temperature 4.500000