Automatically Append host, port no and media folde

2019-09-20 17:36发布

问题:

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:

You can use build_absolute_uri with ImageFields's url attribute:

url = self.request.build_absolute_uri(val.module_img.url)