Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django apps using class-based views and ajax?

Tags:

I'm learning Django and I found class-based views and I wonder how to implement Ajax on those views.

I searched github for a django project and I found some using class-based views but not ajax.

So... Anybody knows an open source project that use both things? It easier to learn that way.

Thank you :)

like image 945
Jesus Rodriguez Avatar asked Nov 09 '11 00:11

Jesus Rodriguez


People also ask

Should I use class based views in Django?

Generic class-based views are a great choice to perform all these tasks. It speeds up the development process. Django provides a set of views, mixins, and generic class-based views. Taking the advantage of it you can solve the most common tasks in web development.

Can you use AJAX with Django?

Using Ajax in Django can be done by directly using an Ajax library like JQuery or others. Let's say you want to use JQuery, then you need to download and serve the library on your server through Apache or others. Then use it in your template, just like you might do while developing any Ajax-based application.

How does class based view work in Django?

A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins.

How send data from AJAX to Django?

To send and receive data to and from a web server, AJAX uses the following steps: Create an XMLHttpRequest object. Use the XMLHttpRequest object to exchange data asynchronously between the client and the server. Use JavaScript and the DOM to process the data.


2 Answers

without using the popular dajaxic and dajax packages, its still a straightforward affair.

It would help to write a decorator that just wraps around django's is_ajax() function for request objects like so:

def ajax_request(function):     def wrapper(request, *args, **kwargs):         if not request.is_ajax():             return render_to_response('error/ajax_required.html', {},                 context_instance=RequestContext(request))         else:             return function(request, *args, **kwargs)     return wrapper 

assuming there is a template called ajax_required to handle this particular failure. Something like this prevents a user from entering your ajax specific url in the browser if thats what you don't want.

Because it makes for a shorter example, the following is a class based ajax view that renders a template.

from django.views.generic.base import TemplateView  class AjaxGeneral(TemplateView):     template_name= None     def get(self, request):         data={}         return render_to_response(self.template_name, data,             context_instance=RequestContext(request))      @method_decorator(ajax_request)     def dispatch(self, *args, **kwargs):         return super(AjaxGeneral, self).dispatch(*args, **kwargs) 

now for everything ajax that just needs to render an html snippet you can define short class based views like:

class ShowSomeTable(AjaxGeneral):     template_name="some_table.html" 

Assuming some_table.html has some html snippet in it.

Now your urls.py entry for this view will look like:

url(r'showtable/$', ShowSomeTable.as_view()), 

and you can call it in the js as normal like:

$(#dynamic-content).load('/showtable'); 
like image 141
yassi Avatar answered Sep 28 '22 07:09

yassi


An ajax view isn't much different to a normal view except that you usually want to return a different format then when processing a normal request. This format is usually JSON.

The documentation has an example of a mixin that can be used to return JSON, so this is a good starting point:

https://docs.djangoproject.com/en/dev/topics/class-based-views/mixins/#more-than-just-html

Do you want your view to reply to normal requests or only deal with AJAX requests? If the former, the only trick would be to write in a small check in the render_to_response method to reject any normal GET requests. If the latter, the above link goes on to discuss a situation where you can create a view that will deal with ajax requests and with normal requests.

like image 42
Timmy O'Mahony Avatar answered Sep 28 '22 05:09

Timmy O'Mahony