[Fixed]-Python django self variable across the class and function

1👍

The problem here is that only one of get and post will be called at a time.

If you want self.email to be available in both the get and post method. You should override the dispatch method. The dispatch method is the method called by the class when the callable entry point as_view class method is called.

class MyView(View):

    def dispatch(self, request, *args, **kwargs):
        email  = get_email()
        self.email = email
        return super(MyView, self).dispatch(request, *args, **kwargs)

0👍

When you send a get request, it is a different instance of MyView than when you send a post request. Every request is a new instance.

Consider rewriting your methods in such a way that you don’t need to store the email like this (e.g. store it in request.session if you must, but there is probably a better way to do this depending on what you’re really trying to do.)

Leave a comment