Hi, I am trying to create a custom content type which support ajax.
The same url should give two template files, one is for normal html page, another is just raw page. I knew I can use request.is_ajax() to verify if the request is an Ajax call or not, but how can I set different template for it?
Do I have to create urls.py to make it happen or use page processors? Could any pro to guild me about this please?
I was thinking about this recently. A mezzanine site that is about to
launch came to me with the front-end pre-built to use ajax for the
entire UX, which gave the project what I considered a very bad "smell"
of premature optimization. Now that the architecture and UX is built
to work on browsers without javascript, I have considered how best to
approach applying that ajax UX (if we really want it) to the existing
codebase. The idea that keeps floating to the top is to add an
request.is_ajax() conditional to mezzanine.pages.views.page and return
the page data as json.
<xiaotian....@butterfly.com.au> wrote:
> Hi, I am trying to create a custom content type which support ajax.
> The same url should give two template files, one is for normal html page, another is just raw page. I knew I can use request.is_ajax() to verify if the request is an Ajax call or not, but how can I set different template for it?
> Do I have to create urls.py to make it happen or use page processors? Could any pro to guild me about this please?
We've been using djaxice for all of our ajax support, but these are in our custom apps.
http://www.dajaxproject.com
Is it bad form to return .json when the page is requesting .html? Not familiar with how request.is_ajax() is used, but sounds like a reasonable approach to just serialize the page object into json conditionally.
-------------------------------------------------
Brian Schott, CTO
Nimbis Services, Inc.
brian.sch...@nimbisservices.com
ph: 443-274-6064 fx: 443-274-6060
On Aug 20, 2012, at 12:04 PM, Ken Bolton <kenbol...@gmail.com> wrote:
> I was thinking about this recently. A mezzanine site that is about to
> launch came to me with the front-end pre-built to use ajax for the
> entire UX, which gave the project what I considered a very bad "smell"
> of premature optimization. Now that the architecture and UX is built
> to work on browsers without javascript, I have considered how best to
> approach applying that ajax UX (if we really want it) to the existing
> codebase. The idea that keeps floating to the top is to add an
> request.is_ajax() conditional to mezzanine.pages.views.page and return
> the page data as json.
> Anyone else have similarly subversive thoughts?
> ken
> On Sun, Aug 19, 2012 at 11:22 PM, Xiaotian Tan
> <xiaotian....@butterfly.com.au> wrote:
>> Hi, I am trying to create a custom content type which support ajax.
>> The same url should give two template files, one is for normal html page, another is just raw page. I knew I can use request.is_ajax() to verify if the request is an Ajax call or not, but how can I set different template for it?
>> Do I have to create urls.py to make it happen or use page processors? Could any pro to guild me about this please?
I can see a couple of optional one-size-fits-all approaches.
In either case, based on middleware checking request.is_ajax is True:
- context data passed to template gets converted to JSON data and returned
as the response.
- list of template names to search for gets prepended with a
<template-name>-ajax.html for each template.
In my own projects I'd want the latter. Many people would want the former.
I suppose we could create two middleware classes, one for each, that aren't
enabled by default.
On Tue, Aug 21, 2012 at 2:04 AM, Ken Bolton <kenbol...@gmail.com> wrote:
> I was thinking about this recently. A mezzanine site that is about to
> launch came to me with the front-end pre-built to use ajax for the
> entire UX, which gave the project what I considered a very bad "smell"
> of premature optimization. Now that the architecture and UX is built
> to work on browsers without javascript, I have considered how best to
> approach applying that ajax UX (if we really want it) to the existing
> codebase. The idea that keeps floating to the top is to add an
> request.is_ajax() conditional to mezzanine.pages.views.page and return
> the page data as json.
> Anyone else have similarly subversive thoughts?
> ken
> On Sun, Aug 19, 2012 at 11:22 PM, Xiaotian Tan
> <xiaotian....@butterfly.com.au> wrote:
> > Hi, I am trying to create a custom content type which support ajax.
> > The same url should give two template files, one is for normal html
> page, another is just raw page. I knew I can use request.is_ajax() to
> verify if the request is an Ajax call or not, but how can I set different
> template for it?
> > Do I have to create urls.py to make it happen or use page processors?
> Could any pro to guild me about this please?