Skip to main content

Posts

Showing posts with the label response

Python super() method and class inheritance

There is one more useful method in python. super()     It helps to handle inheritance of ancestor class. This sounds a bit of messy. So I'll try to explain. You have class A that does some useful things. And you have to use this class A everywhere in your application with adding some piece of functionality. You may inherit this class B(A) and expand it's functionality. Now you can use class B in stead of class A everywhere and get rid of redundant operations. # Main class (parent) class A ( object ) : def __init__ ( self ) : print ( u'class A constructor' ) # Main class ancestor (inheriting main class) class B ( A ) : def __init__ ( self ) : print ( u'class B constructor' ) super ( B , self ) . __init__ ( )     For live example:     NOTE! Code is theoretical for you because it is taken from live project. I'm using many own written methods here. Neither document  not it's methods are not dja...

Django: how to generate json response.

It's a common practice to generate content different from standart HTML output by HttpResponce. Lets consider Example on how to generate JSON response by Django. First of all! Django has built in "other response generators". Most common task is to generate JSON. In general it's extremely easy. Operation called "seriliazation". Official Django docs say: "  Django’s serialization framework provides a mechanism for “translating” Django objects into other formats. Usually these other formats will be text-based and used for sending Django objects over a wire, but it’s possible for a serializer to handle any format (text-based or not). " Anyway an example, helped me a lot. from django . utils import simplejson def some_view ( request ):     result = []    result . append ({ "user" :request.user })     result . append ({ "key" :request.session.key })     return HttpResponse ( simplejson . dumps ( result ), mimetyp...