Reputation: 49
I am getting an error for my html template when using jinja2. Here is the error I get: TypeError: url_for() takes exactly 1 argument (2 given). The error happend in 2 td tags after the endif statement. I tried using an onclick inside a button which is the other way I know how to add a url_for tag.
Here is the template I use:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table align="center" id="comic_list">
{% for value in bobby %}
<tr>
<td> {{ value[0]|safe }} </td>
<td> {{ value[1]|safe }} </td>
<td> {{ value[2]|safe }} </td>
<td> {{ value[3]|safe }} </td>
<td> {{ value[4]|safe }} </td>
<td> {{ value[6]|safe }} </td>
<td> {{ value[7]|safe }} </td>
</tr>
{% endfor %}
<tr>
<td><a href="{{url_for('test', next)}}"><button type="submit" value="Next">Next</button></a></td>
<td><a href="{{url_for('test', prev)}}"><button type="submit" value="Prev">Previous</button></a></td>
</tr>
</table>
</body>
<footer>
<p align="right">Date/Time: <span id="datetime"></span></p>
<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = dt.toLocaleString();
</script>
</footer>
</html>
Here is the python code used:
@app.route('/test')
def test():
current_page = request.args.get('page', 1, type=int)
comic_dic = {}
per_page = 10
bob = create_bob('Book', 'Yes')
end = (current_page * per_page) + 1
if end > len(bob):
end = len(bob)
start = ((current_page - 1) * per_page) + 1
bob[1:] = sorted(bob[1:], key=lambda v: (v.publisher, v.sort, v.character, v.publication_date, int(v.volume)))
bobby = []
bobby.append(bob[0:1])
for result in bob[start:end]:
bobby.append(result)
next = 'page=' + str(current_page + 1)
prev = 'page=' + str(current_page - 1)
comic_dic['bob'] = bobby
comic_dic['next'] = current_page + 1
comic_dic['prev'] = current_page - 1
return render_template('yes.html', bobby=bobby, next=next, prev=prev)
Thanks Zach
Upvotes: 0
Views: 885
Reputation: 1942
Here's the documentation for url_for(): http://flask.pocoo.org/docs/0.12/api/#flask.url_for
Your problem is that url_for only takes one argument (like the error says). It does however take additional keyword arguments. For example, if you wanted to pass the next and prev variables back the way you currently have it, you'd just need to change your code to look like:
<tr>
<td><a href="{{url_for('test', page=next)}}"><button type="submit" value="Next">Next</button></a></td>
<td><a href="{{url_for('test', page=prev)}}"><button type="submit" value="Prev">Previous</button></a></td>
</tr>
This will generate links that look something like:
<tr>
<td><a href="example.com/test?page=3"><button type="submit" value="Next">Next</button></a></td>
<td><a href="example.com/test?page=1"><button type="submit" value="Prev">Previous</button></a></td>
</tr>
I'm assuming, of course, that the first template you show is passed next and prev variables which are 3 and 1 respectively.
Upvotes: 1