I'm looking for some help with my custom user model. For a reason I cannot understand the field id_username pops up after clicking on the submit button.
This field is missing from the form which I have cut down to {{ form.as_ p }} in the html.
login.html
<div class="mx-auto" style="max-width: 30rem;">
<!-- Card -->
<div class="card card-lg mb-5">
<div class="card-body">
<!-- Form -->
<form class="js-validate needs-validation" action="{% url 'login' %}" method="POST">
{% csrf_token %}
<div class="text-center">
<div class="mb-5">
<h1 class="display-5">Sign in</h1>
<p>Dont have an account yet? <a class="link" href="{% url 'accounts:register' %}">Sign up here</a></p>
</div>
<div class="d-grid mb-4">
<a class="btn btn-white btn-lg" href="#">
<span class="d-flex justify-content-center align-items-center">
<img class="avatar avatar-xss me-2" src="{% static '/assets/svg/brands/google-icon.svg' %}" alt="Image Description">
Sign in with Google
</span>
</a>
</div>
<span class="divider-center text-muted mb-4">OR</span>
</div>
<!-- Form -->
<div class="mb-4">
<!-- End Form Check -->
{{ form.as_p }}
<label class="form-label w-100 d-flex">Your email</label>
<span class="invalid-feedback">Please enter a valid email address.</span>
<label class="form-label w-100" tabindex="0">
<span class="d-flex justify-content-between align-items-center">
<span>Password</span>
<a class="form-label-link mb-0" href="./authentication-reset-password-basic.html">Forgot Password?</a>
</span>
</label>
<div class="input-group-merge">
<a id="changePassTarget" class="input-group-append input-group-text" href="javascript:;">
<i id="changePassIcon" class="bi-eye"></i>
</a>
</div>
</div>
<div class="d-grid">
<button type="submit" class="btn-block btn btn-primary">Sign in</button>
</div>
</form>
<!-- End Form -->
</div>
</div>
<!-- End Card -->
<!-- Footer -->
urls.py
path('login/', LoginView.as_view(template_name="registration/login.html"), name='login'),
forms.py
class LoginForm(forms.Form):
email = forms.EmailField(label='Email', widget=forms.TextInput(attrs={'placeholder': 'Email'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}))
views.py
class LoginView(FormView):
form_class = LoginForm
success_url = '/about/'
template_name = 'registration/login.html'
def form_valid(self, form):
request = self.request
next_ = request.GET.get('next')
next_post = request.POST.get('next')
redirect_path = next_ or next_post or None
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user = authenticate(request, username=email, password=password)
if user is not None:
login(request, user)
try:
del request.session['guest_email_id']
except:
pass
if is_safe_url(redirect_path, request.get_host()):
return redirect(redirect_path)
else:
return redirect("/about/")
return super(LoginView, self).form_invalid(form)
models.py
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
help_text='Email Address'
)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False) # a superuser
timestamp = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = [] # Email & Password are required by default.
objects = UserManager()
def get_full_name(self):
# The user is identified by their email address
return self.email
... etc ...
@property
def is_active(self):
"Is the user active?"
return self.active
objects = UserManager()
Visually it can be followed that I click login on accounts/login where you can see the id_email and id_password. On click follows is a form error and the emergence of id_username from the depths of hell.
Can anyone please explain to me why this is possibly happening? It's really driving me nuts.