Search code examples
djangomime-typesdispatcher

Can different Django apps serve the same URL for different MIME types?


I want a URL, say /users/fred, to serve different content based on the Accept header. If the requested MIME type is application/activity+json, I want to serve an ActivityPub representation of Fred's account; otherwise, I want to serve an HTML page.

The twist is that I want to implement the ActivityPub view and the HTML view in different Django apps. If they were at different URLs, this would be easily done with urls.py, but that doesn't seem to allow filtering by Accept.

Is there a standard way to do this? Do I need to implement something new in middleware?


Solution

  • If you mean that you want that calculation done in the urls.py path itself, no you can't. A url path is linked to a single view function (or View.as_view()). These only apply pattern matching on the url itself.

    However, you can certainly link it to a first view function which reads your header and dispatches it to the right app view based on its content.

    e.g. In a urls.py

    url_pattern = [
        ...
        path('/users/<str:username>', views.user_dispatch_view(), name='user-detail'),
    ]
    

    In app1.views.py

    from app2.views import json_view
    
    def html_view(request, **kwargs):
        '''
        Do your stuff, or even use a generic view instead,
        calling it with YourGenericView.as_view(request, **kwargs)
        '''
    
    def user_dispatch_view(request, **kwargs):
        if request.Meta.get('Accept') == application/activity+json:
            return json_view(request, **kwargs)
        return http_view(request, **kwargs)