Re: [mezzanine-users] Give different template for normal page and ajax raw page

97 views
Skip to first unread message

Ken Bolton

unread,
Aug 20, 2012, 12:04:06 PM8/20/12
to mezzani...@googlegroups.com
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
<xiaoti...@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?

Brian Schott

unread,
Aug 20, 2012, 1:13:45 PM8/20/12
to mezzani...@googlegroups.com
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....@nimbisservices.com
ph: 443-274-6064 fx: 443-274-6060

Stephen McDonald

unread,
Aug 21, 2012, 4:36:38 PM8/21/12
to mezzani...@googlegroups.com
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.

What do you guys think?

--
Stephen McDonald
http://jupo.org
Reply all
Reply to author
Forward
0 new messages