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

39 views
Skip to first unread message

Tom Tanner

unread,
Nov 25, 2017, 10:55:08 PM11/25/17
to Django users
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",)

When a user goes to "/signin/", Django loads `sign_in.html`:
 <h2>Log in</h2>
 
<form method="post">
  {% csrf_token %}
  {{ login_form.as_p }}
 
<button type="submit">Log in</button>
 
</form>

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?

Matemática A3K

unread,
Nov 26, 2017, 2:44:44 PM11/26/17
to django...@googlegroups.com
On Sun, Nov 26, 2017 at 12:55 AM, Tom Tanner <dontsende...@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...@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.

mohammad k

unread,
Nov 26, 2017, 2:59:57 PM11/26/17
to django...@googlegroups.com
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})

Tom Tanner

unread,
Nov 26, 2017, 6:57:38 PM11/26/17
to Django users
Adding `exclude=["username"]` does nothing. Same if I replace "username" with "email" or "user".


On Sunday, November 26, 2017 at 2:44:44 PM UTC-5, Matemática A3K wrote:
On Sun, Nov 26, 2017 at 12:55 AM, Tom Tanner <dontsende...@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...@googlegroups.com.

Matemática A3K

unread,
Nov 26, 2017, 9:43:10 PM11/26/17
to django...@googlegroups.com
On Sun, Nov 26, 2017 at 8:57 PM, Tom Tanner <dontsende...@gmail.com> wrote:
Adding `exclude=["username"]` does nothing. Same if I replace "username" with "email" or "user".

it's one or another, you either use 'include' or 'exclude'
 
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages