2017-08-02 4 views
0

djangoが提供するログインフォームを編集したいが、セキュリティ上の問題のために新しいフォームを作成したくない。私はHow to use another field for logging in with Django Allauth?のような他のソリューションを見てきましたが、それは良い例ですが、携帯電話番号に基づいて電子メールIDを割り当てます。しかし、私は特にリダイレクトが行われた入力に基づいて認証する以外のフィールドを追加したい。私は私のアプローチとそうすることが可能かどうかについてはかなり混乱しています。親切にお勧めします。ありがとう。djangoのログインフォームにユーザ名とパスワードとともに新しいフィールドを追加する

答えて

0

これを行うことで、あなたのforms.pyファイルで行うことができます。

class UserLoginForm(forms.Form): 
    username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Username'})) 
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Password'})) 
    yourfield = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'yourfield'})) 
    def clean(self, *args, **kwargs): 
     username = self.cleaned_data.get("username") 
     password = self.cleaned_data.get("password") 

     #user_qs = User.objects.filter(username=username) 
     #if user_qs.count() == 1: 
     # user = user_qs.first() 
     if username and password: 
      user = authenticate(username=username, password=password) 
      if not user: 
       raise forms.ValidationError("This user does not exist") 
      if not user.check_password(password): 
       raise forms.ValidationError("Incorrect password") 
      if not user.is_active: 
       raise forms.ValidationError("This user is no longer active") 
      return super(UserLoginForm, self).clean(*args, **kwargs) 
0

あなたの質問に誤解があった場合にはお詫び申し上げますが、ここではユーザー登録に余分なフィールドを追加しています。私は冗長であることをいくつかの余分な関連メソッドが含まれていました:

../forms.py:

class CustomRegistrationForm(RegistrationForm): 
    """ 
    Form for registering a new user account. 

    Subclasses should feel free to add any additional validation they 
    need, but should avoid defining a ``save()`` method -- the actual 
    saving of collected user data is delegated to the active 
    registration backend. 

    """ 
    username = forms.RegexField(regex=r'^[\[email protected]+-]+$', 
           max_length=30, 
           label="Username", 
           error_messages={'invalid': "This value may contain only letters, numbers and @/./+/-/_ characters."}) 

    email = forms.EmailField(label="E-mail") 
    password1 = forms.CharField(widget=forms.PasswordInput, 
           label="Password") 
    password2 = forms.CharField(widget=forms.PasswordInput, 
           label="Password (again)") 

    extra_field = forms.CharField([field options]) 


    def clean(self): 

     if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: 
      if self.cleaned_data['password1'] != self.cleaned_data['password2']: 
       raise forms.ValidationError("The two password fields didn't match.") 
     return self.cleaned_data 

はその後、単に適切なフォームクラスを使用するようにレジスタのURLを設定します。 ./urls.py:

url(r'^accounts/register/$', RegistrationView.as_view(form_class=accounts.forms.CustomRegistrationForm), name='registration_register'), 

このフィールドは標準モデルの一部ではないのですか、入力に余分な作業が必要ですか? FYI私はジャンゴ登録-Reduxのバックエンドを使用してい

from forms import CustomRegistrationForm 
def user_created(sender, user, request, **kwargs): 
    form = CustomRegistrationForm(request.POST) 
    user_account = get_user_account(user) 
    user_account.persona = form.data['persona_tier'] 
    user_account.save() 

from registration.signals import user_registered 
user_registered.connect(user_created) 

しかし、このアプローチは、あなたが関係なく、閉じて得るのを助ける必要があります:あなたは、ユーザーが登録されたときに、いくつかの余分な魔法を実現するために信号を設定することができます。

関連する問題