简体   繁体   English

django图片url前面的localhost地址

[英]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.当我得到一个图像时,有些模型在开头放了“http://localhost:8000”,有些模型没有。 I want to unify it in one way, but how can I express it without attaching "http://localhost:8000"?我想以一种方式统一它,但是不附加“http://localhost:8000”如何表达呢?

You need to use the build_absolute_uri.您需要使用 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.如果您分享您的代码,我们可能会提供更准确的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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