简体   繁体   English

django休息框架https为绝对网址?

[英]django rest framework https for absolute urls?

The server hosting the api is returning http for absolute urls even though the page was loaded using https, does this have something to do with django rest framework? 托管api的服务器正在为绝对URL返回http,即使页面是使用https加载的,这是否与django rest框架有关? because there doesn't seem any obvious way to remedy this. 因为似乎没有任何明显的方法可以解决这个问题。

It's the url field in the Meta class that is relevant 这是Meta类中相关的url字段

class NewsSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(read_only=True)
    source = serializers.CharField(source='get_source_url', read_only=True)
    comments_count = serializers.IntegerField(read_only=True)
    date_added = serializers.CharField(source='humanize_date_added',
                                       read_only=True)
    is_owner = serializers.SerializerMethodField()
    user_voted = serializers.SerializerMethodField()
    favorited = serializers.SerializerMethodField()
    image = serializers.SerializerMethodField()    

    def create(self, validated_data):
        user = self.context['request'].user
        story = News(user=user, **validated_data)
        story.save()
        return story    

    def get_is_owner(self, obj):
        user = self.context['request'].user
        if user.is_active and user == obj.user:
            return True
        return False    

    def get_user_voted(self, obj):
        user = self.context['request'].user
        if user.is_active:
            return obj.user_voted(user)
        return None    

    def get_favorited(self, obj):
        user = self.context['request'].user
        if user.is_active:
            return obj.is_favorite(user)    


    class Meta:
        model = News
        fields = ('id', 'link', 'title', 'text', 'source', 'user',
                  'date_added', 'image', 'comments_count', 'url',
                  'upvotes', 'downvotes', 'user_voted', 'type',
                  'is_owner', 'favorited')
        read_only_fields = ('date_added')

i am not sure if it has to do with nginx but i have this in the config 我不确定它是否与nginx有关,但我在配置中有这个

proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

You need to make sure nginx forwards the client's request scheme because it'll make a regular http request to Django. 您需要确保nginx转发客户端的请求方案,因为它将向Django发出常规的http请求。 You'll need to add the following line to your vhost definition: 您需要将以下行添加到vhost定义中:

proxy_set_header X-Forwarded-Proto $scheme;

Are you using Rest Framework reverse() function to build URLs? 您是否使用Rest Framework reverse()函数来构建URL? http://www.django-rest-framework.org/api-guide/reverse/ http://www.django-rest-framework.org/api-guide/reverse/

It uses incoming request to determine the protocol used. 它使用传入请求来确定使用的协议。

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

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