Reputation: 425
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?
Upvotes: 0
Views: 131
Reputation: 36
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)
Upvotes: 2