2013-07-01 43 views
1

我想創建在Django一個登錄表單,我用這個方法:如何創建自己的登錄表單的Django

def Login(request): 
    if request.method == 'POST': 
     form = AuthenticationForm(request.POST) 
     if form.is_valid: 
      username = request.POST['username'] 
      password = request.POST['password'] 

      access = authenticate(username=username, password=password) 
      if access is not None: 
       if access.is_active: 
        login(request,access) 
        return HttpResponseRedirect('/') 
       else: 
        return render_to_response('noactive.html', context_instance=RequestContext(request)) 
      else: 
       HttpResponseRedirect('/') 
    else: 
     form = AuthenticationForm() 
    return render_to_response('login.html', {'form':form}, context_instance=RequestContext(request)) 

有什麼辦法來創建一個表單的表單中forms.py創建而不是Django提供給你的django.contrib.auth.forms.AuthenticationForm?

+0

class MyForm(AuthenticationForm)? –

+0

你爲什麼想這樣做? Django的貢獻形式做了很多 – karthikr

+0

但是,如果我想讓我的用戶獲得更多的信息,如「出生日期」或「血型」? django.contrib.auth.forms沒有給你什麼東西? –

回答

1

所以,我認爲你混淆了兩個不同的東西:一個是你想問的用戶。第二個是你想要存儲在數據庫中的。

第一個問題在viewsforms執行,後者在models執行,特別是User profiles

通常情況下,表單與您要執行的操作相關聯。插入新內容或修改現有內容(包括用戶登錄)。

所以,我對你的問題的回答是:你應該嘗試閱讀我添加的這4個鏈接。您必須瞭解視圖的底層思想,即服務器對用戶請求的響應,表單作爲工具用於修改帶有用戶輸入的數據庫而不會損壞數據庫,以及將模型視爲以結構化方式存儲信息的數據結構。

希望這會有所幫助。

相關問題