一个Django rest framework开发的web服务,需要支持iOS的universal link功能。
需要将apple-app-site-association文件放在域名的目录下。实现xxx.xx/apple-app-site-association即可访问下载。
需要处理三处。

1、django的setting配置静态文件

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,MEDIA_URL)

2、nginx的conf配置location

location /static {
    alias /home/project/app/static;
}

3、Views中实现get方法(重要)

class apple_association_view(APIView):
authentication_classes = []
permission_classes=[]
def get(self,*args, **kwargs):
    # 指定需要下载的文件
    static_root = settings.STATIC_ROOT
    download_file = os.path.join(static_root, 'apple-app-site-association')

    response = HttpResponse(download_file)
    response['Content-Type'] = 'application/octet-stream' #设置头信息,告诉浏览器这是个文件
    response['Content-Disposition'] = 'attachment;filename="apple-app-site-association"'
    return response

最后配置url即可
path('apple-app-site-association',apple_association_view.as_view()),

标签: nginx, django, uwsgi, drf, universal, link, apple-app-site-association

添加新评论