2011-05-25 200 views
0

請問我的英文不是最好的。Django課堂問題

我對django和python很陌生,我嘗試編程用戶認證。 我用Django文檔,一切工作正常用下面這些代碼:

def anges(request): 

     username = [] 
     password = [] 

     if request.method == "POST": 
       username = request.POST['username'] 
       password = request.POST['password'] 
       user = authenticate(username=username, password=password) 
       render_to_response ('registration/login.html', {'username': username, 'password': password}) 

       if user is not None: 
         if user.is_active: 
           login(request, user) 
           angestellte_list = Employee.objects.all().order_by('lastname') 
           return render_to_response(("emp/angestellte.html"), {'angestellte_list': angestellte_list}) 
         else: 
           return HttpResponse('disabled account') 
       else: 
         return HttpResponse('invalid login') 
     else: 
       return render_to_response ('registration/login.html', {'username':username, 'password':password}) 

但是,這僅僅是一個功能,我想使用面向我在我的views.py等功能此對象,因爲乾的。 這只是第一個測試,但它不工作,因爲調試器說:「全球名‘請求’沒有定義」 這是我的代碼:

class einloggen: 

     def __init__(self): 
       self.Username = request.POST['username'] 

     def angestellte(self): 
       return HttpResponse("hello") 

我如何使用請求變量的類或還有什麼需要考慮的嗎?

+1

Django已經有一個完整的用戶認證應用程序。爲什麼寫自己的? – 2011-05-25 12:05:54

+0

...或者換句話說,你需要什麼'django.contrib.auth'缺乏? – 2011-05-25 12:16:02

回答

2

很明顯,你不能在einloggen類中使用request變量__init__,因爲,坦率地說,你不會將請求變量發送給構造函數。

我看不到你在視圖作出einloggen對象無論在任何地方,但你應該喜歡的東西:

class einloggen: 
    def __init__(self, request): 
     self.Username = request.POST.get('username') 

,然後在您的視圖(你已經得到了請求變量) :

def anges(request): 
    myobj = einloggen(request) 

然而,Django的已經有一個認證系統。使用它你會好得多。你可以使用美麗的裝飾,使它真的很容易,很好«保護»意見。