[Answer]-Matching ContentType as a string, get invalid literal for int() with base 10

1👍

Your Post model is expecting id to be an integer, so change your URL pattern to:

url(r'^post/(?P<id>\d+)/$', 'single_post', name='single_post'),

You’ll need to filter the Vote model by a ContentType model instance, not by the string ‘vote’:

from django.contrib.contenttypes.models import ContentType

def single_post(request, id):
    post = get_object_or_404(Post, id=id)
    . . .
    content_type = ContentType.objects.get_for_model(vote)

    if request.user.is_authenticated():
        user_rating = Vote.objects.get(user=user, content_type=content_type,
            object_id=id)
    . . .

When you look up an object by foreign key reference, Django expects you to provide an object to do the comparison, unless you specify a property, like __id, etc.

Leave a comment