views:

46

answers:

2

so I was wondering if you could return an inclusion tag directly from a view.

The page is a normal page with a list of items. The list of items is rendered using an inclusion tag. If an ajax request is made to the view, I want to return only what the inclusion tag would return so I can append it onto the page via javascript. Is something like this possible? Or should I architect this better?

A: 

This is certainly possible. A view can render any template that an inclusion tag can. The advantage of this approach is that you can leverage the full strength of Django's templates.

On the other hand AJAX responses typically do not contain HTML so much as XML/JSON. If you are using Django's template features you are better off with returning HTML. If not, XML/JSON might make more sense. Just my two cents.

Manoj Govindan
I've always had a problem with returning xml/json and then having javascript render the data into html. Isn't that violating the DRY principles cause now we have the code duplicated in the django template and in the javascript.
You mention it can be done, but you don't include an example. Can you show me how to do it or a page that has it?
A: 

quick and dirty way could be to have a view, that renders a template, that only contains your templatetag.

dysmsyd
ya, this way seemed to be redundant in code, was hoping for something that didn't involve that extra step.
then expose the serialised models as json , check out http://code.google.com/p/django-rest-interface/ for a start, or roll your own app, use a different URL namespace rather than branching in your existing views though!
dysmsyd