Django.fun

Associating a post with a user in react and django using JTW tokens

I would like to associate a post with a logged-in user, a react, and Django app. I have tried this, and am getting an unauthorized error, settings.py

REST_FRAMEWORK = {
# 'DEFAULT_PERMISSION_CLASSES': [
#     'rest_framework.permissions.IsAuthenticated'
# ],
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework_simplejwt.authentication.JWTAuthentication',
),

}

SIMPLE_JWT = {
'AUTH_HEADER_TYPES': ('JWT',),
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'AUTH_TOKEN_CLASSES': (
    'rest_framework_simplejwt.tokens.AccessToken',
)

}

my serializers.py

class PoultryCreateSerializer(serializers.ModelSerializer):
class Meta:
    model = Poultry
    fields = ('title', 'price', 'category', 'description',
              'image', 'county', 'subcounty', 'location', 'contact')
    read_only_fields = ['seller']

views.py

@api_view(["POST"])
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
def poultrycreate(request):
print(request.user)
parser_classes = (MultiPartParser, FormParser)
serializer = PoultryCreateSerializer(data=request.data)
if serializer.is_valid():
    serializer.save(seller=request.user)
    return Response(serializer.data, status=status.HTTP_201_CREATED)
# print('error', serializer.errors)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Answers: 0