Django rest framework https for absolute urls?

The server hosting the api returns HTTP for absolute URLs, even if the page was loaded using https, does this have something to do with the django rest framework? because there is no obvious way to fix this.

This is the url field in the Meta class, relevant

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'm not sure if this is related to nginx, but I have it in config

proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
+1
source share
2 answers

You need to make sure that nginx redirects the client request scheme because it will make a normal Django HTTP request. You will need to add the following line to your vhost definition:

proxy_set_header X-Forwarded-Proto $scheme;
+6
source
0

Source: https://habr.com/ru/post/1015769/


All Articles