[Fixed]-Access class variables in functions – Django

1👍

You have to use self. In front of class variables.

Your function names in class based views should correspond to what http method you try to use(get, post etc…)

@login_required
def get(self,request):

    return render(request,
          'app/submit.html',{'my_strings':self.my_strings, 'var2':self.var2})

Please also read:
https://docs.djangoproject.com/en/1.9/topics/class-based-views/intro/

👤Eska

0👍

You don’t have to write custom function to dispatch request…Django internally have the GET and POST method to do that… And also preferred way to handle login required is method_decorator

from django.utils.decorators import method_decorator 

@method_decorator(login_required, name='dispatch')
class MyView(View):

    string = "your string"

    def dispatch(self, *args, **kwargs):
        return super(MyView, self).dispatch(*args, **kwargs)

    def get(self, request):
        return render(request, 'template', {'string': self.string})

Leave a comment