DRF web interface doesn't work as Postman
Question:
I’am creating a blog app where only authenticate users can add blogs. I’m using JWT token and on Postman it work’s well and I can add blog’s after I logged in. I want to do it on DRF web interface also but it always tell me that I’m not logged in – I think.
Exception Value:
Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x0000021035CA1C50>": "Blog.author" must be a "User" instance.
My views.py
class BlogList(generics.ListCreateAPIView):
queryset = Blog.objects.all()
serializer_class = BlogSerializer
authentication_classes = [JWTAuthentication]
#permission_classes = [IsAuthenticated]
permission_classes = [IsOwnerOrReadOnly]
filter_backends = [SearchFilter]
search_fields = ["title", "content", "author__username", "author__first_name", "author__last_name"]
def perform_create(self, serializer):
serializer.save(author=self.request.user)
Serializer.py
class BlogSerializer(serializers.ModelSerializer):
author = serializers.ReadOnlyField(source="author.username")
author_first_name = serializers.ReadOnlyField(source="author.first_name")
author_last_name = serializers.ReadOnlyField(source="author.last_name")
slug = serializers.ReadOnlyField()
comments = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
read_only_fields = ["author", "first_name", "last_name"]
class Meta:
model = Blog
fields = ["id", "title", "content", "status", "author", "comments", "image",
"created_on", "updated_on", "author_first_name", "author_last_name", "slug"]
def to_representation(self, instance):
data = super().to_representation(instance)
user = self.context["request"].user
if not user.is_authenticated:
data.pop("slug")
return data
def save_author(self, **kwargs):
if "author" not in self.validated_data:
self.validated_data["author"] = self.context["request"].user
return super().save(**kwargs)
def save(self,**kwargs):
self.validated_data["slug"] = slugify(self.validated_data.get("title"))
return super().save(**kwargs)
I don’t know what else I should post here. I tried somehow to DRF web interface add some button like "Authenticate" where you can post your acces token but I can not solve it.
Answers:
If you use JWT tokens to authenticate users you can download some browser extension that can modify request headers and add them. Then you paste your token there, give header a name and you are good to go.
Here is the one I personally use:
https://chrome.google.com/webstore/detail/modheader-modify-http-hea/idgpnmonknjnojddfkpgkljpfnnfcklj
p.s. can cause some issues on other websites if you don’t turn it off, so keep that in mind.
I’am creating a blog app where only authenticate users can add blogs. I’m using JWT token and on Postman it work’s well and I can add blog’s after I logged in. I want to do it on DRF web interface also but it always tell me that I’m not logged in – I think.
Exception Value:
Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x0000021035CA1C50>": "Blog.author" must be a "User" instance.
My views.py
class BlogList(generics.ListCreateAPIView):
queryset = Blog.objects.all()
serializer_class = BlogSerializer
authentication_classes = [JWTAuthentication]
#permission_classes = [IsAuthenticated]
permission_classes = [IsOwnerOrReadOnly]
filter_backends = [SearchFilter]
search_fields = ["title", "content", "author__username", "author__first_name", "author__last_name"]
def perform_create(self, serializer):
serializer.save(author=self.request.user)
Serializer.py
class BlogSerializer(serializers.ModelSerializer):
author = serializers.ReadOnlyField(source="author.username")
author_first_name = serializers.ReadOnlyField(source="author.first_name")
author_last_name = serializers.ReadOnlyField(source="author.last_name")
slug = serializers.ReadOnlyField()
comments = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
read_only_fields = ["author", "first_name", "last_name"]
class Meta:
model = Blog
fields = ["id", "title", "content", "status", "author", "comments", "image",
"created_on", "updated_on", "author_first_name", "author_last_name", "slug"]
def to_representation(self, instance):
data = super().to_representation(instance)
user = self.context["request"].user
if not user.is_authenticated:
data.pop("slug")
return data
def save_author(self, **kwargs):
if "author" not in self.validated_data:
self.validated_data["author"] = self.context["request"].user
return super().save(**kwargs)
def save(self,**kwargs):
self.validated_data["slug"] = slugify(self.validated_data.get("title"))
return super().save(**kwargs)
I don’t know what else I should post here. I tried somehow to DRF web interface add some button like "Authenticate" where you can post your acces token but I can not solve it.
If you use JWT tokens to authenticate users you can download some browser extension that can modify request headers and add them. Then you paste your token there, give header a name and you are good to go.
Here is the one I personally use:
https://chrome.google.com/webstore/detail/modheader-modify-http-hea/idgpnmonknjnojddfkpgkljpfnnfcklj
p.s. can cause some issues on other websites if you don’t turn it off, so keep that in mind.