Search code examples
pythondjangodjango-1.6

reason of exception invalid literal for int() with base 10:


When I try go to the category category1-test i have exception : invalid literal for int() with base 10: 'category1-test' i dont understand reason of this exception.

MODELS

class Category(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=500)
    slug = models.SlugField(max_length=300, unique=True)

    def __str__(self):
        return '%s%s' % (self.title, self.description)

    class Meta:
        verbose_name_plural = 'Categories'

    @models.permalink
    def get_absolute_url(self):
       return ('category-detail', (), {'category_id': str(self.slug)})





class Info(models.Model):
    ...
    category = models.ForeignKey(Category)

    @models.permalink
    def get_absolute_url(self):
        return ('category_info', (), {'info_id': str(self.id)})

VIEWS

def view_list(request, category_id):
    list = Info.objects.filter(category = category_id).order_by("-created")
    list = pagination(request, list, 10)
    category = get_object_or_404(Category, slug=category_id)
    return render_to_response('view_list.html', add_csrf(request, list=list, slug=category_id, category=category), context_instance=Reque
stContext(request))


Traceback:
File "/usr/local/lib/python3.2/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/usr/info/views.py" in view_list
  41.     list = Info.objects.filter(category = category_id).order_by("-created")
File "/usr/local/lib/python3.2/dist-packages/django/db/models/manager.py" in filter
  163.         return self.get_queryset().filter(*args, **kwargs)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/query.py" in filter
  590.         return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/query.py" in _filter_or_exclude
  608.             clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python3.2/dist-packages/django/db/models/sql/query.py" in add_q
  1198.         clause = self._add_q(where_part, used_aliases)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/sql/query.py" in _add_q
  1232.                     current_negated=current_negated)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/sql/query.py" in build_filter
  1122.                                                      lookup_type, value)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/fields/related.py" in get_lookup_constraint
  1105.                      value[index]), AND)
File "/usr/local/lib/python3.2/dist-packages/django/utils/tree.py" in add
  104.         data = self._prepare_data(data)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/sql/where.py" in _prepare_data
  79.             value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/sql/where.py" in prepare
  352.             return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  369.             return self.get_prep_value(value)
File "/usr/local/lib/python3.2/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
  613.         return int(value)

Exception Type: ValueError at /category1-test Exception Value: invalid literal for int() with base 10: 'category1-test'


Solution

  • category_id appears to be a string slug (e.g. "category1_test") that is extracted from the url, but you are trying to use it as a foreign key (int). You could do this:

    lst = Info.objects.filter(category__slug=category_id).order_by("-created")
    

    or, rearranging:

    category = get_object_or_404(Category, slug=category_id)
    lst = Info.objects.filter(category=category).order_by("-created")
    

    (Note: I would not assign to the name "list" if I were you. You're hiding the builtin list!)