Sunday, November 26, 2017

Re: How do I limit my login form to just email and password?

forms.py :

class LoginUserForm(forms.Form):
    email = forms.CharField(
        widget=forms.EmailInput(attrs={'autofocus': True, 'placeholder': 'Email'}),
        required=True,
        label='Email :',
        max_length=255
    )
    password = forms.CharField(
        widget=forms.PasswordInput(attrs={'placeholder': 'Password'}),
        label=_('Password :'),
        required=True,
        max_length=50,
    )

    def clean(self, *args, **kwargs):
        email = self.cleaned_data.get('email', None)
        password = self.cleaned_data.get('password', None)
        if email and password:
            try:
                loginuser = User.objects.get(email=email)
            except ObjectDoesNotExist:
                raise forms.ValidationError(_('Email or Password are Wrong!'))
            if not loginuser.is_active:
                raise forms.ValidationError(_('This User Does not Active.'))
            if not loginuser.check_password(password):
                raise forms.ValidationError(_('Email or Password are Wrong!'))
            registration = Registration.objects.filter(user=loginuser, is_registered=True)
            if not registration.exists():
                reg_email(
                    to_list=[loginuser.email],
                    subject='Email Registration for djangotutorial.ir',
                    code=signing.dumps({'id': loginuser.id}, salt=salt) + '/?reg_code=' + signing.dumps({'username': loginuser.username, 'id': loginuser.id, 'email': loginuser.email}, salt=salt)
                )
                raise forms.ValidationError(_('This Account is not Registered!. Email has been sent. check your email'))
            login(self.request, loginuser)
        return super(LoginUserForm, self).clean()

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(LoginUserForm, self).__init__(*args, **kwargs)


views.py : 

class LoginUser(FormView):
    """Generate User Login Page By http://site_domain/login/ Based on public/registration/login.html Template"""
    template_name = 'public/registration/login.html'
    form_class = LoginUserForm
    success_url = reverse_lazy('home')

    def get(self, request, *args, **kwargs):
        """When The Get Request Incoming."""
        if request.user.is_authenticated:
            return redirect(self.success_url)
        return render(request, self.template_name, {'form': self.form_class()})

    def post(self, request, *args, **kwargs):
        """When The Post Request Incoming. User Submit Login Form."""
        next_url = request.GET.get('next', None)
        form = self.form_class(request.POST or None, request=request)
        if form.is_valid():
            if next_url:
                return redirect(next_url)
            return redirect(self.success_url)
        return render(request, self.template_name, {'form': form})

On Sun, Nov 26, 2017 at 11:13 PM, Matemática A3K <matematica.a3k@gmail.com> wrote:


On Sun, Nov 26, 2017 at 12:55 AM, Tom Tanner <dontsendemailhereok@gmail.com> wrote:
My `models.py` has this:
class MyUser(AbstractBaseUser):
 email
= models.CharField(max_length=254, unique=True)
 USERNAME_FIELD
= "email"

My `forms.py` has this:
class LoginForm(AuthenticationForm):
 email
= forms.EmailField(label=_("Email"), max_length=254)

 
class Meta:
  model
= MyUser
  fields
= ("email",)

The form on the template has three fields: Username, Password and Email, in that Order. I want only Email and Password to show up in the form. How do I do that?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4e9a3109-3dd0-4754-91f2-44731154920c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhLt-rz93nqAhOYE_VgWPFCU2pCKjL7qZu2z0u9%3DkBxY-w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACOk0Ty4tjuCOovpLmjTKvPY5cApA0U7QhG-Fs%2B8A7bzU1P1Sg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment