Create dynamic URLs in Flask with url_for()
It takes keyword arguments for the variables:
url_for('add', variable=foo)
url_for('remove', variable=foo)
The flask-server would have functions:
@app.route('/<variable>/add', methods=['GET', 'POST'])
def add(variable):
@app.route('/<variable>/remove', methods=['GET', 'POST'])
def remove(variable):
Pass dynamic url parameter for flask url_for
Pass it as second argument to url_for
{{ url_for('home.downloads', application_name = "YOUR DYNAMIC APP NAME") }}
Edit:
For the dynamic one, you simply pass the variable.
{{ url_for('home.downloads', application_name = application) }}
and when you are rendering the template, send your dynamic value.
application = "DO WHATEVER AND ASSIGN THE VALUE HERE"
return render_template("template.html", application = application )
Refer: https://flask.palletsprojects.com/en/2.1.x/quickstart/#url-building
Flask - Creating dynamic URL based on user input
You are coming at it from the wrong way. When Jinja creates index.html based on your template you can't know what variable
is going to be, so how is Jinja supposed to know?
What you need to do is send the form back to the same route and then redirect the user to whatever they typed in. So something like this:
from flask import request, redirect, url_for
@app.route("/", methods=['GET', 'POST'])
def markert_hours_today():
if request.method == 'POST':
result = request.form['variable']
return redirect(url_for('ticker_result', variable=result)
return render_template("index.html")
Edit: forgot to rename variable from user_input to result.
Create dynamic arguments for url_for in Flask
Any arguments that don't match route parameters will be added as the query string. Assuming extra_args
is a dict, just unpack it.
extra_args = {'hello': 'world'}
url_for('doit', oid=oid, **extra_args)
# /doit/123?hello=world
url_for('doit', oid=oid, hello='davidism')
# /doit/123?hello=davidism
Then access them in the view with request.args
:
@app.route('/doit/<int:oid>')
def doit(oid)
hello = request.args.get('hello')
...
how to create a dynamic url from form using flask
The answer was to add an if statement to the form. Otherwise it errors out
@app.route("/", methods=['POST', 'GET'])
def home():
if request.form:
project_select = request.form['project']
return redirect(url_for("project_page", project=project_select))
else:
return render_template("home.html", list_projects=list_projects)
Best method for dynamically building URL with Flask url_for
If the elements of last_orders
contain the prefix used in the xxx_page
function(s), then you can just make an a
tag within the td
.
<td><a href={{ url_for('{}_page'.format(x)) }}>something here</a></td>
EDIT: After rereading your question, you're looking to make the entire cell clickable. There are a couple options in JavaScript. You can listen to clicks on all the td
elements, and then trigger a click on their child a
.
# Don't actually use an anonymous function. Define it elsewhere.
$('body').off('click.order')
.on('click.order', 'td', function(e) {
$(this).find('a').trigger('click');
});
OR (and I like this option much less than the first one) you could set a data element on the td
elements with the address.
<td data-url='{{ url_for('{}_page'.format(x)) }}'>something here</td>
Then in your click
listener, do this.
window.location.href = $(this).attr('data-url');
Related Topics
Tensorflow Different Ways to Export and Run Graph in C++
Display a 'Loading' Message While a Time Consuming Function Is Executed in Flask
Scrape Multiple Pages with Beautifulsoup and Python
How to Generate an HTML Directory List Using Python
Python Beautifulsoup Iframe Document HTML Extract
Django: How to Serve Media/Stylesheets and Link to Them Within Templates
Style Active Navigation Element with a Flask/Jinja2 MACro
Color Coding Cells in a Table Based on the Cell Value Using Jinja Templates
How to Pull Out CSS Attributes from Inline Styles with Beautifulsoup
Multiple Level Template Inheritance in Jinja2
Control the Size Textarea Widget Look in Django Admin
Python How to Parse CSS File as Key Value
How to Highlight Searched Queries in Result Page of Django Template
Using Beautiful Soup to Convert CSS Attributes to Individual HTML Attributes
Best Way to Set Entry Background Color in Python Gtk3 and Set Back to Default
I Can't Import Python Modules in Xcode 11 Using Pythonkit