托管api的服务器正在为绝对URL返回http,即使页面是使用https加载的,这是否与django rest框架有关?因为似乎没有任何明显的方法可以解决这个问题。
这是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')
我不确定它是否与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;
答案 0 :(得分:11)
您需要确保nginx转发客户端的请求方案,因为它会向Django发出常规的http请求。您需要在vhost定义中添加以下行:
proxy_set_header X-Forwarded-Proto $scheme;
答案 1 :(得分:0)
您是否使用Rest Framework reverse()函数来构建URL? http://www.django-rest-framework.org/api-guide/reverse/
它使用传入请求来确定使用的协议。