codingforentrepreneurs / Blog-API-with-Django-Rest-Framework

Blog API with Django Rest Framework
MIT License
356 stars 173 forks source link

IsAuthenticated Permission is not working #6

Open anjadriano16 opened 6 years ago

anjadriano16 commented 6 years ago

When creating a post and there's no user logged in, the result shows "Authentication credentials were not provided", something like that. When I'm trying to create a post and there's no user logged in, it doesn't show the message above. It still proceed in creating.

What do I need to do?

This the code the I used.

from rest_framework.generics import ( CreateAPIView, DestroyAPIView, ListAPIView, UpdateAPIView, RetrieveAPIView, RetrieveUpdateAPIView )

from rest_framework.permissions import ( AllowAny, IsAuthenticated, IsAdminUser, IsAuthenticatedOrReadOnly,

)

from posts.models import Post

from .permissions import IsOwnerOrReadOnly

from .serializers import ( PostCreateUpdateSerializer, PostDetailSerializer, PostListSerializer )

class PostCreateAPIView(CreateAPIView): queryset = Post.objects.all() serializer_class = PostCreateUpdateSerializer permission_classes = [IsAuthenticated]

def perform_create(self, serializer):
    serializer.save(user=self.request.user)

class PostDetailAPIView(RetrieveAPIView): queryset = Post.objects.all() serializer_class = PostDetailSerializer lookup_field = 'slug'

lookup_url_kwarg = "abc"

class PostUpdateAPIView(RetrieveUpdateAPIView): queryset = Post.objects.all() serializer_class = PostCreateUpdateSerializer lookup_field = 'slug' permission_classes = [IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]

lookup_url_kwarg = "abc"

def perform_update(self, serializer):
    serializer.save(user=self.request.user)
    #email send_email

class PostDeleteAPIView(DestroyAPIView): queryset = Post.objects.all() serializer_class = PostDetailSerializer lookup_field = 'slug'

lookup_url_kwarg = "abc"

class PostListAPIView(ListAPIView): queryset = Post.objects.all() serializer_class = PostListSerializer

4vadim4 commented 3 years ago

Edit the question, it's impossible for reading ! Where is the code? Where is the text? I mean, the writing's atrocious.