Pass JavaScript Variable to Flask Url_For

Pass JavaScript variable to Flask url_for

You can't evaluate JavaScript in Jinja. You're trying to generate a url on the server side while Jinja is rendering, but you're referencing a variable that is only available in the JavaScript running on the client browser.

Building the url on the client side is the most straightforward fix. (I don't know what your route looks like, so here's an example.)

$('#demo').load('/url/for/addshare2/' + variable1);

However, this isn't very useful because you can't use url_for, so you have to hard-code the urls. This is a good sign that what you want is an AJAX endpoint that you pass parameters to, rather than an endpoint that contains values.

@app.route('/addshare2', methods=['POST'])
def addshare2():
share = request.json['share']
...
return jsonify(result=...)

Now you can generate the url with url_for, and pass the parameters as form data.

$.post(
'{{ url_for('addshare2') }}',
{share: variable1},
function (data) {
// do something with data on successful response
}
);

how to pass js variable to flask jinja function url_for

You can't pass javascript variables to Jinja2 because of the way the template gets rendered.

Server side processing is done before client side.

Here is the order.

  • Jinja2 processes the template into proper markup.

  • The browser parses the markup into DOM and renders it.

The way this works only allows for passing Jinja2 variables to Javascript.

You need to do without using url_for Jinja2 directive and build your URL client side.

var tmp=$("#cellphone").val();
console.log(tmp);
$.getJSON(["<url_path for auth.send_sms>", tmp].join("/"),
function(data){
});

Flaskr url_for with javascript variable as parameters is not working to python

Short answer: you can't. Flask & Jinja2 render the template on the server side (e.g. Flask is translating all of the {{ }} stuff before it sends the HTML to the web browser).

For a URL like this where you're including a variable as part of the path you'd need to build this manually in javascript. If this is an XHR endpoint I'd recommend using GET/POST to transfer the values to the server as a better best practice than constructing the URL this way. This way you can use Jinja:

$(document).ready(function(){
var baseUrl = "{{ url_for('disable') }}";

$('.dissable_user_btn').click(function(event) {
var user_id = $(this).data("user_id");

// first part = url to send data
// second part = info to send as query string (url?user=user_id)
// third parameter = function to handle response from server
$.getJSON(baseUrl, {user: user_id}, function(response) {
console.log(response);
});
});
});

pass the url with variable param from javascript to flask endpoint

You are mixing backend and frontend.

In this snippet

$.getJSON({{ url_for('scrape.start_scrape', domain=domain_v)|tojson}}, {})

All inside these curly braces {{}} are python side code. And the variable domain_v is frontend Javascript variable which you cannot pass backend function.

You have 2 options to fix this.
Either you defined domain_v in the backend with URL parsing functionality too.
Or you can remove url_for('scrape.start_scrape', domain=domain_v) and do the same in Javascript side.

how to pass variable in url to python flask

Try it like this:

$http.post("/CA/" + currenttopic, date).success(function(response) {

or with ES6:

$http.post(`/CA/${currenttopic}`, date).success(function(response) {

Flask send variable data to python through url_for()

url_for is expanded on the server side, before the page gets sent to the browser and well before JavaScript runs.

If you know the number (for device/data.value) at page generation time, pass it in to the template via render_template().

If, however, you don't know the value until after the page is rendered, you're going to need to construct the img element from JavaScript.

How to insert a JavaScript variable into Flask url_for() function?

Try this:

cell1.innerHTML = '<a href={{ url_for('static', filename='') }}' + id + '.txt>' + id + '</a>';

url_for() will generate an URL like this: .../static/<filename>. If you use url_for('static', filename=''), it generate an URL like: .../static/, so you can just add text after it (i.e. + id + '.txt>') .



Related Topics



Leave a reply



Submit