[Answer]-How do I return variables from a view and have that output either a template or raw JSON based on context?

1👍

Typically, with RESTful APIs, this sort of thing is handled with content type negotiation. I haven’t used it, but Django REST framework can provide content negotiation.

0👍

Have a single function responsible for collating the info, and then two separate views responsible for calling the common function and either rendering a template or encoding as JSON.

Leave a comment