Automatically Append host, port no and media folde

2019-09-20 18:12发布

I'm using Django rest framework APIView method to retrieve image field (Image saved in local and name saved in DB). Is there any way to append Host, Port and media Folder automatically. Now i'm appending manually. But if i'm using Viewset it appended automatically. Both method i'm using for different scenario. Can anyone suggest some way to solve the issues.

for val in ModuleList.objects.filter(user_type_id=user_type):
     os.path.join(settings.MEDIA_URL, str(val.module_img))

This is how i'm appending url manually

1条回答
Emotional °昔
2楼-- · 2019-09-20 18:29

You can use build_absolute_uri with ImageFields's url attribute:

url = self.request.build_absolute_uri(val.module_img.url)
查看更多
登录 后发表回答