简体   繁体   中英

The localhost address in front of the django image url

在此处输入图片说明

在此处输入图片说明

When i get an image, some models put "http://localhost:8000" at the beginning and some models don't. I want to unify it in one way, but how can I express it without attaching "http://localhost:8000"?

You need to use the build_absolute_uri. I do it the following way:

object_serialized = ModelSerializer(object)
        object_data = object_serialized.data.copy()
        if object_data['goal_image']:
            object_data['goal_image'] = request.build_absolute_uri(
                object.goal_image.url)

        return Response(object_data)

If you share your code, we may be able to provide more accurate advise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM