I try to make a button with a javascript "location.href" property in Django ListView templates.
{% for item in object_list %}
<div class="clear_float">
<h2><a href = "{% url 'photo:album_detail' item.id %}">{{ item.title }}</a></h2>
 <b><i>{{ item.description }}</i></b>
<p style="display:inline" class="editbutton">
<b><button onclick="photoAdd()">Add Photo</button></b>
{% if item.author == request.user %}
<b><button onclick="albumEdit()">Edit</button></b>
<b><button onclick="albumDelete()">Delete</button></b>
{% endif %}
</p>
</div>
{% endfor %}
And albumEdit() and albumDelete() is coded like below.
function albumEdit(){
location.href="{% url 'photo:album_edit' object.id %}";
}
function albumDelete(){
location.href="{% url 'photo:album_delete' object.id %}";
}
{% url 'photo:album_edit' object.id %} returns "photo/object.id/edit/"
The problem is, I guess, the ListView returns object_list so the javascript function is used in FOR loop with item object. I tried replacing object.id with item.id but it didn't work.
So I also tried another way to give a argument to javascript function like below.
<b><button onclick="albumEdit({% url 'photo:album_update' item.id %})">Edit</button></b>
and javascript
function albumEdit(arg){
var address = arg;
location.href=encodeURI(address);
}
Cuz I don't know much about javascript, it also didn't work. I will be grateful for any advice and help.
Thank you :D
<b><button onclick="location.href='{% url 'photo:album_edit' item.id %}'">Edit(</button></b>