Yawd website

Articles Tagged class-based-views

  1. Using class-based views to process a django form through ajax

    Field-specific errors with the JSONFormView class-based view Since Django 1.3 class-based views have been introduced to allow for code reusability. Class-based views have been criticized for their poor documentation (count me in) and their amazing ability to grow our code size instead of reducing it! The latter appears to be true only if we are not careful with sub-classing. On the other hand class-based views finally deliver django what has been missing: a consistent way of coding all your models, forms and views.
    Unfortunately, django's generic class-based views do not provide a mechanism for implementing JSON views. Ajax requests are the standard way of doing things these days and I feel that django needs some work towards this direction. However, the official documentation offers the implementation of a simple JSONResponseMixin that we will use as a base for our own view. What we try to achieve here is create a view that will process a form, return the errors when the form is not valid or save the form and return a success message otherwise. This is a fairly common scenario and extremely useful when a full page reload is not acceptable (e.g. a newsletter subscription form).
    Posted on August 30th, 2011 by Pantelis Petridis