Reputation: 8029
I have a view like:
class MyView(View):
def get(self, request):
email = get_email()
self.email = email
return
def post(self, request, **kwargs):
if not self.email in kwargs['email']
return redirect(some_url)
return
As I know assigning a value in self make is global to the class. In my get I am assigning email to the self.email that should be accessed by post too. But its now working. In post I cannot get self.email
Can anyone guide me through this ?
Upvotes: 0
Views: 502
Reputation: 747
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.)
Upvotes: 0
Reputation: 4043
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)
Upvotes: 1