Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有ImageFIeld和HTTPS的Django ModelSerializer_Django_Rest_Serialization_Https_Django Rest Framework - Fatal编程技术网

带有ImageFIeld和HTTPS的Django ModelSerializer

带有ImageFIeld和HTTPS的Django ModelSerializer,django,rest,serialization,https,django-rest-framework,Django,Rest,Serialization,Https,Django Rest Framework,我有一个带有图像的模型新闻,这些新闻可以通过JSON REST API加载。服务器使用来自权威机构的证书进行签名,所有请求都必须使用https完成 我的问题是,ModelSerializer使用http而不是https序列化ImageField。我该如何改变这一点 以下是代码摘要和输出示例: #myProject/models.py class News(models.Model): image = models.ImageField() #myProject/serializers.

我有一个带有图像的模型新闻,这些新闻可以通过JSON REST API加载。服务器使用来自权威机构的证书进行签名,所有请求都必须使用https完成

我的问题是,ModelSerializer使用http而不是https序列化ImageField。我该如何改变这一点

以下是代码摘要和输出示例:

#myProject/models.py
class News(models.Model):
    image = models.ImageField()

#myProject/serializers.py
class NewsSerializer(serializers.ModelSerializer):
    class Meta:
        model = News
        fields = ('image')

#request for a news
https://myDomain/news/the-news-id-here/

#current output
{
    "image": "http://myDomain/media/news/imageName.jpg"
}

#wanted output
{
    "image": "https://myDomain/media/news/imageName.jpg"
}
谢谢
David

您可以在nginx配置中执行此操作,如下所示

server {
    listen         80;
    return 301 https://$host$request_uri;
}

请考虑添加<代码> PROXYSETIGHORD X-PROTEDD PROTO HTTPS;<代码>位于Nginx虚拟主机文件内,即位于

/etc/Nginx/sites available/
内的conf文件。因此,简而言之,您的conf文件可能如下所示:

server {
    listen   443 ssl;
    server_name example.com www.example.com;
    root /var/www/html/static_files/;

    client_max_body_size 4G;
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    access_log /home/user/API/logs/nginx-access.log;
    error_log /home/user/API/logs/nginx-error.log;

    location /api/ {
        proxy_pass http://127.0.0.1:8000/api/;
    }

    location /media/ {
        proxy_pass http://127.0.0.1:8000/media/;
    }

    # Error pages
    # error_page 500 502 503 504 /home/user/API/500.html;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

综上所述,添加
proxy\u set\u头X-Forwarded-Proto-https后。您的REST API将重定向到https。感谢他的评论。

生成的响应中使用的方案是根据其自身用于请求的方案动态确定的。您正在通过HTTPS查询服务器吗?您还将在开发服务器上获得不同的行为,开发服务器将始终使用HTTP。还要检查您是否使用通过HTTP与Django对话的内部代理,在这种情况下,您需要设置仍然不起作用,即使使用标题也是如此。我正在使用Django 1.11。编辑:您需要在Django设置中放入
SECURE\u PROXY\u SSL\u HEADER=('HTTP\u X\u FORWARDED\u PROTO','https')
,感谢您突出显示该设置