Issue
The variable is defined inside get_context_view()
since it requires an id
to access correct database object:
class FooView(TemplateView):
def get_context_data(self, id, **kwargs):
---
bar = Bar.objects.get(id=id)
---
def post(self, request, id, *args, **kwargs):
# how to access bar?
# should one call Bar.objects.get(id=id) again?
What would be the way to pass bar
variable to post()
?
Tried to save it as FooView's field and access it via self.bar
, but this doesn't do the trick. self.bar
is not seen by post()
Solution
You should reverse it. If you need bar
in post()
, you need to create it there:
class FooView(TemplateView):
def get_context_data(self, **kwargs):
bar = self.bar
def post(self, request, id, *args, **kwargs):
self.bar = Bar.objects.get(id=id)
...
post()
is called before get_context_data
, that's why post
doesn't see it if you define it in get_context_data
.
Answered By - knbk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.