ValueError at /api/list/ invalid literal for int() with base 10: '35/like'

36 views
Skip to first unread message

mounikesh mintu

unread,
Aug 9, 2018, 11:29:03 PM8/9/18
to Django users
hey guys when i tried to like  a post in drf i have got in to an error

models.py
class status(models.Model):
    user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=False)
    # user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)

    content=models.TextField(blank=True,null=True)

    image=models.ImageField(upload_to="upload",null=True,blank=True)
    updated=models.DateTimeField(auto_now=True)
    time=models.DateTimeField(auto_now_add=True)
    likes=models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name='post_likes')

    def __str__(self):
        return self.content
serilaizers

serializers.py

class statusSerializer(serializers.ModelSerializer):
    # user_name = serializers.ReadOnlyField(source='user.username')
    user=UserPublicSerializer(read_only=True)
    # rating=RatingSerializer(required=False)
    # rating=RatingSerializer()

    class Meta:
        model=status
        fields=['id','user','content','image','likes']

views.py

@api_view(['POST'])
def LikesApi(self,request,id):
    # status = get_object_or_404(id=request.POST.get('id', ''))
    status = get_object_or_404(id=id)

    status.likes.add(request.user)
    serializer = statusSerializer(status)
    return Response(serializer.data, status=status.HTTP_201_CREATED)

i have tried both the ways with regards of that question from stack

@api_view(['POST'])
def LikesApi(self,request,id):
    serializer=statusSerializer(data=request.DATA)
    if serializer.is_valid():
        serializer.object.content_object=get_object_or_404(status,id=request.POST.get('id', ''))
        serializer.object.likes.add(request.user)
        serializer.save()
        return  RestResponse(serializer.data, status=status.HTTP_201_CREATED)
    return RestResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

here is my urls.py

urlpatterns = [
        # url(r'^$/', StatusListSearchApi.as_view()),
        url(r'^list/', StatusListSearchApi.as_view()),
        url(r'^user/', UserPost.as_view()),

        url(r'^like/(?P<id>\d+)/$',LikesApi),

post objects are present in my api/list/ url

http://127.0.0.1:8000/api/list/like?id=35

when i fired this url i cant see any incremination of likes in my browasable api detailview

http://127.0.0.1:8000/api/like?id=35

tried this as well but rather got a page not found 404 error

but when i tried 

this url pattern


i have got that value error 

please can anyone help me

Andréas Kühne

unread,
Aug 10, 2018, 3:26:34 AM8/10/18
to django...@googlegroups.com
Hi,

See my comments below.

Regards,

Andréas

The url endpoints you have registered are:
/api/list/
/api/user/

and /api/like/<id>/

where id is a part of the url.

So to like a post with the id 35 you would enter:
/api/like/35/
 
http://127.0.0.1:8000/api/list/like?id=35

when i fired this url i cant see any incremination of likes in my browasable api detailview

http://127.0.0.1:8000/api/like?id=35

tried this as well but rather got a page not found 404 error

but when i tried 

this url pattern


i have got that value error 

please can anyone help me

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a64dbbfd-ace5-4186-9c2b-801c66bd7804%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages