Re: Multiple data formats for one view

2009-08-05 Thread Ludwik Trammer
If you do this just for unintrusive AJAX functionality you can simply use HttpRequest.is_ajax() http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.is_ajax if request.is_ajax(): # return data serialized to JSON or XML else: # render data in HTML template

Re: Multiple data formats for one view

2009-08-05 Thread Sam Lai
I have a piece of middleware which assigns the right MIME type based on URL extension, then I have templates for XML, JSON and HTML. My view function simply gets the required context objects, then passes it to the appropriate template. This could probably be generified to make it more reusable.

Re: Multiple data formats for one view

2009-08-05 Thread krylatij
Why not? You can simply specify mimetype in HttpResponse object urls.py (r'^articles.xml/$', my_view_function, {'format': 'xml'}), (r'^articles.html/$', my_view_function, {'format': 'html'}), views.py def my_view_function(request, format='json'): if format == 'xml': mimetype = '.'

Multiple data formats for one view

2009-08-05 Thread Dmitry Gladkov
Hello, What is the best Django-way to create multiple data formats for one view as in Ruby On Rails? For example, "articles.json" and "articles.xml" will serialize response object to json or xml representation, and articles.html will render to articles.html templ