1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

django rest framework で pngファイルをgetで受け取る方法

Posted at

django templateで開発した後にcomponentsを使ってspa開発をすると
色々うまくいかなくてかなり大変ですね。。。

今回はdjango✖️spaを使ってgetで受け取る時に詰まった箇所の備忘録を書きます。

view.js
class AuthInfoGetView(generics.RetrieveAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = User.objects.all()
    serializer_class = AccountSerializer

    def get(self, request, format=None):
            return Response(data={
            'username': request.user.username,
            'icon': request.user.image,
            },
            status=status.HTTP_200_OK)

このようなget関数を使って特定のアカウントのイメージファイルを指すと

'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

とエラーが出ます

これは、imageファイル自体を指す事であってimageファイルまでのpathを指すことができていないので、エラーが出てしまっています。

なので

'icon': request.user.image,

'icon': request.user.image.url

に変えると動きました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?