1

this is my delete_category function :

def delete_category(request, id):
    user = request.user
    category = Category.objects.get(pk=id)
    category.user.remove(user)
    return HttpResponseRedirect(reverse('auth_profile', args=[user.username]))

this is my category_detail function:

def category_detail(request, category_name):
    user = request.user
    category = Category.objects.filter(name=category_name, user__username=user)[0]
    return render_to_response("category_detail.html", { "category":category}, context_instance=RequestContext(request))

this is category_detail.html:

        <p>{{category.name}}</p>
        <a href="{% url 'delete_category' category.id %}">delete</a>

If I have more than one category, I can delete when I click the delete link. But if I have only one category, I get this error:

list index out of range

Amy Obrian
  • 1,183
  • 2
  • 11
  • 19
  • where did you put the delete_category function? – Fernando Freitas Alves Dec 04 '13 at 18:15
  • For your future sanity's sake, don't allow `GET` requests to modify your database! That way lies terrible, terrible side effects. See http://stackoverflow.com/questions/705782/why-shouldnt-data-be-modified-on-an-http-get-request . That said, how are you linking to your `category_detail` view? Also, can you doublecheck your posted code? As it stands your view function takes a parameter `shelf_name`, but references an undeclared variable `category_name`. Assuming that's not an actual error, that view will throw that exception if the user and category_name pair don't match an object. – Peter DeGlopper Dec 04 '13 at 18:16
  • thanks a lot I will be more careful – Amy Obrian Dec 04 '13 at 18:25

2 Answers2

3

change

category = Category.objects.filter(name=category_name, user__username=user)[0]

by

categories = Category.objects.filter(name=category_name, user__username=user)
category = None
if categories:
   category = categories[0]

see also how to work properly with QuerySet objects here

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
1

your code is a little hard to follow, i suspect this is not the exact code, but that's ok.

list index out of range is referring to the ...[0] you specified.

you may want to rewrite what you've got to better handle zero categories, but a quick fix is:

try:
    category = Category.objects.filter(name=category_name, user__username=user)[0]
except:
    category = None
...
jpwagner
  • 553
  • 2
  • 8
  • and now I get Reverse for 'delete_category' with arguments '('',)' and keyword arguments '{}' not found. Im confused. – Amy Obrian Dec 04 '13 at 18:24
  • this is crazy because if I have more than one category I can delete but if I have only one category I got error. – Amy Obrian Dec 04 '13 at 18:26
  • you need to rewrite your code to better handle the case where you have one or zero categories – jpwagner Dec 04 '13 at 18:27
  • with no category passed in you get `{% url 'delete_category' %}`...you should write an if statement in the templating language... – jpwagner Dec 04 '13 at 18:29
  • while I am on A category_detail_page if I try to delete this page I got error. – Amy Obrian Dec 04 '13 at 18:33
  • I not recommend use a try ... except block for do this. That line of code may fail for any other reason, not only "list index out of range" – Raydel Miranda Dec 04 '13 at 19:22