Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  60] [ 1]  / answers: 1 / hits: 53582  / 12 Years ago, wed, april 25, 2012, 12:00:00

What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what is the recommended way to do this in .js files? Since they are not interpreted by the template engine.



What basically want to do is:



// in comments.js
$.post(url_for('comment.comment_reply'));


Which is not possible.



But naturally, I can execute that in a template:



<script>
$.post(url_for('comment.comment_reply'));
</script>

More From » python

 Answers
152

What @dumbmatter's suggesting is pretty much considered a de facto standard way. But I thought there would be a nicer way of doing it. So I managed to develop this plugin: Flask-JSGlue.



After adding {{ JSGlue.include() }}, you can do the following in your source code:



<script>
$.post(Flask.url_for('comment.comment_reply', {article_id: 3}));
</script>


or:



<script>
location.href = Flask.url_for('index', {});
</script>

[#85989] Tuesday, April 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kerryoliviaa

Total Points: 221
Total Questions: 102
Total Answers: 117

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;