I have the following view in my views.py:
def MoveSucessfulEntries(request):
if request.method == 'POST':
objectsToBeDeleted = request.POST['imagesToBeRemoved']
for objects in objectsToBeDeleted:
print objects
image = Image.objects.get(id=objects)
SucessfulImage(emailAddress=image.emailAddress, image=image.image, caption=image.caption).save()
return render_to_response('images/selectedSucessful.html')
else:
images = Image.objects.all()
return render_to_response('images/deleteUnsucessfulEntries.html', {'images': images})
imagesToBeRemoved are checkboxes on the HTML page that have the IDs of the images in the database which is generated like so:
<form method="POST" action="/image/selectSucessful">
{% if images %}
{% for image in images %}
<input type="checkbox" name="imagesToBeRemoved" value="{{image.id}}" /> <image src="../media/{{image.image}}"> <br>
{%endfor%}
<input type="submit" value="Select" />
{% else %}
<p>No images are available.</p>
{% endif %}
</form>
So I know that the ids returned from the form are in the database yet I keep getting the error object does not exist. Whats wrong with my code?
The stack trace says that the error is happening on:
image = Image.objects.get(id=objects)
The issue is you’re getting a string with
Since you’re POSTing multiple data what you want is:
Strings are iterable so you iterate over each of the digits, that is, if you want to delete an item with an id of 345, it’ll actually try to delete the items with ids of 3, 4, and 5.