I have a table ('like') to like post
class Likes(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
i have a table course :
class Courses(models.Model):
title = models.CharField(max_length=100, null=True)
description = RichTextUploadingField(null=True)
like = GenericRelation(Likes)
i use restman opera extension to send POST request to my api if i login with browser i get error
"detail": "CSRF Failed: CSRF token missing or incorrect."
but i use restman only(i dont login with browser) every thing is OK
setting.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication'
]
}
view:
@api_view(['POST'])
@login_required
def f_like(request):
r = {'data': None}
id_o = request.POST.get('id')
type_o = request.POST.get('type')
if(type_o in {'Courses', 'Course_Sessions', 'Course_Session_Exercise'} and id_o.isdigit()):
model = eval(type_o)
if(obj := model.objects.filter(id=id_o)):
obj = obj[0]
a = ['title', obj.title]
if(c2 := obj.like.filter(user=request.user)):
c = c2[0]
c.delete()
a.append(0)
else:
obj.like.create(user=request.user)
a.append(1)
r['data'] = a
return Response(r)
In
setting.py
in MIDDLEWARE try to remove/comment this line:This will disable CSRF validation.