How to redirect a user to a mobile app (without browser) using Django (Universal Link / App Link) with .well-known configuration?

I am working on a Django-based web application, and I need to implement a feature where users are redirected to a mobile app directly (without using a browser) when they visit a specific URL. I want to use Universal Links (for iOS) or App Links (for Android) and ensure the redirect happens seamlessly.

My setup: URL pattern: I am using a URL like /api/app/login/str:session_token/. Mobile app URL: I want to redirect users to a custom scheme URL like myapp://path/to/page/. Current view: This is my view that tries to handle the redirection:

from django.http import HttpResponseRedirect
from django.views import View

class MobileRedirectView(View):
    def get(self, request, *args, **kwargs):
        # Custom mobile app URL (Universal Link for iOS or App Link for Android)
        mobile_app_url = "myapp://path/to/page/"

        # Redirecting to the mobile app
        return HttpResponseRedirect(mobile_app_url)

The problem: When trying to redirect to the mobile app URL using the custom scheme (myapp://), I get this error: django.core.exceptions.DisallowedRedirect: Unsafe redirect to URL with protocol 'myapp'.

I know that Django by default only allows redirects to http:// or https:// URLs, but I need to allow custom schemes like myapp://.

Goal: I want the redirection to happen without using a browser (directly into the mobile app), and I also want to use Universal Links / App Links for iOS and Android respectively.

Additional Context: I have set up .well-known files to configure my app to handle these links on mobile. The .well-known configuration for my iOS and Android apps looks like this: For iOS (inside apple-app-site-association):

{
  "applinks": {
    "details": [
      {
        "appID": "TEAM_ID.com.myapp",
        "paths": ["/path/to/page/*"]
      }
    ]
  }
}

For Android (inside assetlinks.json):

{
  "applinks": {
    "details": [
      {
        "appID": "com.myapp",
        "paths": ["/path/to/page/*"]
      }
    ]
  }
}

How can I allow a redirect to a custom scheme like myapp:// in Django, while ensuring it’s handled directly by the mobile app (iOS/Android) without using a browser? Should I configure Django settings to allow custom scheme redirects? If yes, how? Do I need to modify the Django view or use JavaScript to trigger the redirect in a way that avoids the browser? Can I use .well-known files to ensure that iOS and Android correctly handle these links without any issues? Is there a better approach to achieve this using Universal Links and App Links in a Django app, ensuring the user is redirected directly to the mobile app (not through the browser)? Additional Configuration: Is there any special Nginx or other server-side configuration I need to set up to allow these kinds of redirects on the server? Any help would be greatly appreciated!

Вернуться на верх