Answer the question
In order to leave comments, you need to log in
Pass random value from get method to post method Django?
Let's say I have a view that inherits from the View class
class SomeVeiwClass(View):
def get_random_number(self):
return random.randint(1, 100)
def get(self, request):
context = {
'some_number': self.get_random_number()
}
return render(request, 'some_template.html', context)
def post(self, request):
context = {
'some_number': self.get_random_number()
}
return render(request, 'some_template.html', context)
TypeError: get_random_number() missing 1 required positional argument: 'self'though I call in the same class. I tried to pass a variable through a hidden input in html, but then it can be changed, and I need to make sure that the user cannot influence it in any way. I know that you can just write the result of calling the get_random_number method through shelve to some file when calling the get method, and then read from the same file when calling the post method, but I doubt the speed of such an operation :(
Answer the question
In order to leave comments, you need to log in
In the get method, store the random value in the session, and in the post, retrieve it from it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question