python - Proper usage of Django Mixins -
so started porting old code class based views, , still new @ that. question related django mixins, have mixin going utilize in various classes validate information. problem don't know how access info returns without getting 500 internal error.
class checktokenmixin(object): def request_token(self,request): params = {'username':settings.oauth_username,'password':hashlib.sha256(settings.oauth_password).hexdigest()} req = request(settings.remote_server+'oauth', urllib.urlencode(params)) homecoming json.loads(urlopen(req).read()) def get_valid_token(self): if datetime.datetime.now() > request.session['access_token'].creation_date + datetime.timedelta(days=1): temp = self.request_token(request) tokenobj = accesstoken.objects.all()[:1].get() tokenobj.access_token = temp['token'] tokenobj.creation_date = datetime.datetime.now() tokenobj.save() request.session['access_token'] = tokenobj homecoming request.session['access_token'] def get_context_data(self, **kwargs): ctx = super(checktokenmixin, self).get_context_data(**kwargs) ctx['access_token'] = self.get_valid_token() homecoming ctx class rateappview(loginrequiredmixin, jsonresponsemixin, ajaxresponsemixin, checktokenmixin, view): @method_decorator(csrf_protect) def dispatch(self, *args, **kwargs): homecoming super(rateappview, self).dispatch(*args, **kwargs) def post_ajax(self, request, username): u = get_object_or_404(user, pk=current_user_id(request)) city_obj = city.objects.get(id=request.post.get('city_id', none)) x = self.get_valid_token print "teste: " , x.access_token print "teste2: " , self.get_context_data.['access_token'].access_token
i want
self.get_valid_token
or
self.get_context_data.['access_token'].access_token
to access info mixin, how do proper way?
self.get_valid_token()
correct. however, have several errors in method, no uncertainty causing problem: in particular, refer request
without defining @ point. expect mean self.request
instead.
python django mixins
No comments:
Post a Comment