0

我正在嘗試編寫一個webapp,它允許用戶根據興趣匹配並滿足其他用戶。我處於早期階段,並已撰寫了註冊頁面,並使用自定義用戶模型來註冊用戶。我希望這些用戶能夠登錄他們的個人資料並進行必要的更改/查看他們的個人資料。爲自定義Django用戶創建登錄頁面?

我目前的登錄頁面始終只是刷新,因爲它似乎無法登錄到任何配置文件,即使憑據很好。

自定義用戶型號:

class MyUser(AbstractBaseUser): 
    """ 
    Custom user class. 
    """ 
    first_name = models.CharField(max_length=40) 
    last_name = models.CharField(max_length=40) 
    email = models.EmailField('email address', unique=True) 
    date_of_birth = models.DateField() 
    joined = models.DateTimeField(auto_now_add=True) 
    course = models.ForeignKey(Course) 
    location = models.ForeignKey(Location) 
    interests = models.ManyToManyField(Interest) 
    bio = models.TextField(blank=True) 
    is_active = models.BooleanField(default=True) 
    is_admin = models.BooleanField(default=False) 

    USERNAME_FIELD = 'email' 

    def __unicode__(self): 
     return self.email 

當前登錄的形式:

class AuthenticationForm(forms.Form): 
    """ 
    Login form 
    """ 
    email = forms.EmailField(widget=forms.TextInput) 
    password = forms.CharField(widget=forms.PasswordInput) 

    class Meta: 
     fields = ['email', 'password'] 

最後,當前登錄的看法:

def login(request): 
    if request.method == 'POST': 
     form = AuthenticationForm(data=request.POST) 
     if form.is_valid(): 
      user = authenticate(email=request.POST['email'], password=request.POST['password']) 
      if user is not None: 
       if user.is_active: 
        django_login(request, user) 
        return redirect('/success/') 
    else: 
     form = AuthenticationForm() 
    return render_to_response('login.html', { 
     'form': form, 
    }, context_instance=RequestContext(request)) 

我已經在這個主題上做了很多搜索,但是所有的項目例子或者對這個主題的幫助似乎都相當過時。

任何幫助,將不勝感激!非常感謝。

+0

那麼,如果表單有效,但沒有匹配的用戶或用戶不活躍會發生什麼?沒有任何形式錯誤,但是無論如何你都會遇到並重新顯示錶單。 – 2014-12-03 14:16:14

+2

在任何情況下,您都應該使用內置的[AuthenticationForm](https://docs.djangoproject.com/en/1.7/topics/auth/default/#django.contrib.auth.forms.AuthenticationForm)在表單驗證本身中進行驗證/主動檢查。 – 2014-12-03 14:19:19

回答

0

django在創建&維護用戶方面一直很有幫助。 Have a look at this link. 請注意,如果它的工作與否。 :-)