2013-10-17 4 views
7

में अनुरोध ऑब्जेक्ट प्राप्त करें मैं वर्तमान में उपयोगकर्ता द्वारा कक्षा-आधारित दृश्य में लॉग इन करना चाहता हूं। मैं अनुरोध ऑब्जेक्ट से उपयोगकर्ता को निकालने के द्वारा ऐसा कर सकता हूं, लेकिन मैं उस ऑब्जेक्ट को कैसे प्राप्त कर सकता हूं?कक्षा-आधारित दृश्य

class HomeView(TemplateView): 
    template_name='home.html' 

    def get_context_data(self, **kwargs): 
     context = super(HomeView, self).get_context_data(**kwargs) 
     context['username'] = request.user.username 
     return context 

उत्तर

14

आप उदाहरण आप

पर

if self.request.user.is_authenticated(): 
    ... 

या

context['username'] = self.request.user.username 
    ... 

और आपके CBV में यह कर सकते हैं के लिए से self.request.user

यह करने के लिए उपयोग कर सकते हैं

संबंधित मुद्दे