I’m getting this error: invalid literal for int() with base 10: 'social' when I try to open a page at /category/social/.
def all_partners(request,category):
p = Content.objects.filter(category_id=category)
return render_to_response('reserve/templates/category.html', {'p':p},
context_instance=RequestContext(request))
class ContentCategory(models.Model):
content_category = models.CharField('User-friendly name', max_length = 200)
def __unicode__(self):
return self.content_category
class Content(models.Model):
category = models.ForeignKey(ContentCategory)
external = models.CharField('User-friendly name', max_length = 200, null=True, blank=True)
host = models.CharField('Video host', max_length = 200, null=True, blank=True)
slug = models.CharField('slug', max_length = 200, null=True, blank=True)
def __unicode__(self):
return self.slug
url(r'^category/(?P<category>[-\w]+)/$', 'all_partners'),
Any ideas on how to fix this? I think the error is in the "p = Content..." line.
Within your view,
categoryhas to be an integer, or a string which can be casted to an int likeint('5'). You must be going to a url which does not restrict the category to be an integer:So if the last part of the url maps to the
categoryparameter, then within the view, within the query, it tries to castsocialto an integer, which raises an error.To solve it you either have to restring the url pattern to only allow numbers or change how the query is done.
or
which will then lookup the categories by the name, not by the id.