0

I'm trying to create a little job board, each job card is dynamically inserted from flask from a list (for now, will be an SQL DB later)

Screenshot

When the user clicks the button View Task I want it to go to another page where it displays the task/job in more detail.

What I want attached to that button is the job/task ID.

This is how I'd want/expect the button to function

{% for task in available_tasks %}
<div>
   <a href="{{ url_for('view_task', task_id={{ task['id'] }}) }}">View Task</a>
</div>
{% endfor %}

This could then be pasted into a route that would take the ID as an argument and fetch the full job/task information.

app.route('/view_task/<task_id>')
def view_task(task_id):
   task_data = task_database[taskid]
   return render_template('detailed_view.html', task_info=task_data)

My problem is href="url_for('view_task', task_id={{ task['id'] }})" doesn't work, is there a way to do this?

4
  • Yes, that's how it normally is done. I'm not sure what you are asking; a better way to send back dynamic data is hugely broad. Commented Apr 6, 2019 at 14:16
  • Its not working though? href="url_for('view_task', task_id={{ task['id'] }})" it doesn't recognize the arguement. I could do task_id='1' doesnt seem to like the jinja {{ }} Commented Apr 6, 2019 at 14:16
  • Ah, yes, you got the syntax mixed up a little. But you didn't tell us that you had an error. Please show the full error message, with traceback if possible, for problems. Commented Apr 6, 2019 at 14:19
  • And since you don't have an error here, but just incorrect output, it'd have been helpful if you had shown that instead. Do look at the HTML that Flask returns to the browser, not just what the browser has rendered as a result. You'd have seen the href="..." value was a bit off, not containing an actual URL. Commented Apr 6, 2019 at 14:22

1 Answer 1

2

You generated the output href="url_for('view_task', task_id=<some id>)", so the literal text url_for(..) is in the HTML generated and delivered to your browser, and HTML engines don't recognise that string as a valid URL for anything.

You need to put the url_for() function in the {{ ... }} brackets, not just the task id value:

href="{{ url_for('view_task', task_id=task['id']) }}"

Only then is url_for() actually treated as an expression for Jinja2 to execute, at which point the HTML produces will contain the href="http://localhost:5000/view_task/<some id>" string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.