I'm trying to create a new user in my Django app but nothing happens. I'm using a custom user auth model. Part of the code I edited from the docs. Why the error message "Users must have an email address" is reported by the model and not the forms? Why am I not able to create a user? I don't get any error back.
My model:
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
from django.utils import timezone
class MyUserManager(BaseUserManager):
def create_user(self, email, name, neighborhood, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
name=name,
neighborhood=neighborhood
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, name, neighborhood, password):
user = self.create_user(
email=email,
name=name,
password=password,
neighborhood=neighborhood
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
name = models.CharField(max_length=255)
email = models.EmailField(max_length=255, unique=True)
created_at = models.DateTimeField(default=timezone.now, blank=True)
neighborhood = models.CharField(max_length=255)
consultant_id = models.IntegerField(null=True)
moip_id = models.IntegerField(null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'neighborhood']
def __str__(self):
return self.name
def get_full_name(self):
return self.name
def get_short_name(self):
return self.name
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
#property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
My form:
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from dashboard.models import MyUser
class UserCreationForm(forms.ModelForm):
password = forms.CharField(label='Senha', widget=forms.PasswordInput)
confirm_password = forms.CharField(label='Confirmar senha', widget=forms.PasswordInput)
class Meta:
model = MyUser
# Note - include all *required* MyUser fields here,
# but don't need to include password and confirm_password as they are
# already included since they are defined above.
fields = ('email', 'name', 'neighborhood',)
def clean(self):
cleaned_data = super(UserCreationForm, self).clean()
password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password')
if password and confirm_password and password != confirm_password:
raise forms.ValidationError('As senhas nao batem.')
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password'])
if commit:
user.save()
return user
And my view:
from django.shortcuts import render
from frontend.forms import UserCreationForm
# Create your views here.
def register(request):
message = None
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'frontend/register.html', {'message': message})
So far I know, you do not raise error from forms, you just -
1) add the error in it, then it automatically gets invalided by django and is posted back with error and also
2) since you are overriding the clean method you must return the cleaned data. So change the clean method with these details -
def clean(self):
cleaned_data = self.cleaned_data
password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password')
if password and confirm_password and password != confirm_password:
#raise forms.ValidationError('As senhas nao batem.') => we do not raise error in form clean, instead we add it in validation error.
self.add_error('confirm_password', 'As senhas nao batem.')
return super(UserCreationForm, self).clean() # =>this line is IMPORTANT to not break the calling hierarchy
a little shorter -
def clean(self):
if self.cleaned_data['password'] != self.cleaned_data['confirm_password']:
self.add_error('confirm_password', 'Password & Confirm Password must match.')
return super().clean()
Sine you are not returning anything, the cleaned_data of your form is empty and thus django is returning you back to the form page with no data in it.
Related
Please help. The signup page keeps showing this field is required error. I'm a beginner. thankyouu
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, email, username, full_name, password=None):
if not email:
raise ValueError("Users must have an email adress")
if not username:
raise ValueError("Users must have a username")
if not password:
raise ValueError("Users must have a password")
if not full_name:
raise ValueError("Users must have a name")
user = self.model(
email=self.normalize_email(email), # return lowercase
username=username,
full_name=full_name,
)
user.set_password(password) # users set password and change password
user.full_name = full_name
user.save(using=self._db)# using the default database in settings.py
return user
def create_superuser(self, email, username, full_name, password=None):
user = self.create_user(
email=self.normalize_email(email), # return lowercase
username=username,
full_name=full_name,
password=password,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db) # using the default database in settings.py
return user
class Account(AbstractBaseUser):
email = models.EmailField(
verbose_name="email",
max_length=60,
unique=True,
)
username = models.CharField(max_length=30, unique=True)
full_name = models.CharField(max_length=30, blank=True, null=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
profile_image = models.ImageField(max_length=255, null=True, blank=True, default=default_profile)
hide_email = models.BooleanField(default=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username", "full_name"]
objects = UserManager() # tie account to my custom user manager
def __str__(self):
return self.username
# "Does the user have a specific permission?"
def has_perm(self, perm, obj=None):
return True
# "Does the user have permissions to view the app `app_label`?"
def has_module_perms(self, app_label):
return True
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout, authenticate
from account.forms import RegistrationForm, AccountAuthenticationForm
def register_view(request):
user = request.user
if user.is_authenticated:
return redirect('home')
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
email = form.cleaned_data.get('email').lower()
raw_password = form.cleaned_data.get('password1')
user = authenticate(email=email, password=raw_password)
login(request, user)
return redirect('home')
else:
form = RegistrationForm()
return render(request, 'account/register.html', {'registration_form': form})
def login_view(request):
context = {}
user = request.user
if user.is_authenticated:
return redirect("home")
if request.POST:
form = AccountAuthenticationForm(request.POST)
if form.is_valid():
email = request.POST.get('email')
password = request.POST.get('password')
user = authenticate(request, email=email, password=password)
if user is not None:
login(request, user)
return redirect("home")
else:
form = AccountAuthenticationForm()
context['login_form'] = form
return render(request, "account/login.html", context)
def logout_view(request):
logout(request)
return redirect("home")
forms.py
from django import forms
from django.contrib.auth import get_user_model
from .models import Account
from django.contrib.auth import authenticate
User = get_user_model()
class RegistrationForm(forms.ModelForm):
full_name = forms.CharField()
email = forms.EmailField(max_length=60, help_text='Required. Add a valid email address.')
password1 = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(widget=forms.PasswordInput, label='Confirm password')
class Meta:
model = Account
fields = (
'email',
'username',
'full_name',
'password1',
'password2',
)
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data.get('email')
user.username = self.cleaned_data.get('username')
user.full_name = self.cleaned_data.get('full_name')
if commit:
user.save()
return user
def clean_email(self):
email = self.cleaned_data.get('email')
qs = User.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError(f"Email {email} is already taken")
return email
def clean_username(self):
username = self.cleaned_data.get('username')
qs = User.objects.filter(username=username)
if qs.exists():
raise forms.ValidationError(f"Username {username} is already taken")
return username
def clean(self):
data = self.cleaned_data
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password2 != password1:
raise forms.ValidationError("Passwords must match")
return data
class AccountAuthenticationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
class Meta:
model = Account
fields = ('email', 'password')
def clean(self):
if self.is_valid():
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
if not authenticate(email=email, password=password):
raise forms.ValidationError("Incorrect username or password")
The cause of the problem could be due to the username field and email field being used one in place of the other.
An example found in the code mentioned above:
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username", "full_name"]
The problem can be resolved by verifying and fixing these errors where the email field is used in place of username field and vice versa.
Most likely there is a error in your fields html id and name so the form validation registers it as an empty value
if request.method == "POST":
updated_request = request.POST.copy()
updated_request.update({'username': request.POST['email']})
form = RegistrationForm(updated_request)
Why doing something so simple seems so complicated in Django? It started with setting up a user model where email is used to login, a pain... Now I am trying to save a user as user.is_active = False but it is saved as active. Any tips very welcome or any other guidance as Django has been quite a painful experience so far compared to Flask.
Note, I have also tried #receiver(pre_save, sender=get_user_model()) below.
views.py
...
from .models import Story, MyUser
from .forms import SignupForm, LoginForm
from django.dispatch import receiver
from django.db.models.signals import pre_save
#receiver(pre_save, sender=MyUser)
def set_new_user_inactive(sender, instance, **kwargs):
if instance._state.adding is True:
print("Creating Inactive User")
instance.is_active = False
else:
print("Updating User Record")
...
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
user = get_user_model()
user.objects.create_user(
form.cleaned_data['username'],
form.cleaned_data['email'],
form.cleaned_data['password'],
)
return HttpResponseRedirect(reverse('index'))
else:
form = SignupForm()
return render(request, 'drillapp/signup.html', {'form': form})
...
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
...
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
)
self.username = username
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password):
user = self.create_user(
email,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
objects = MyUserManager()
USERNAME_FIELD = 'email'
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
Why don't you do like:
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
is_active = models.BooleanField(
default=False
)
I successfully manually sent an email to specific email address and activated the user but I cannot figure out how to:
Set submitted emails into recipient_list in my
post_save_activation_receiver signal, since email field itself is defined in other model (if it is possible)?
Make this emails that I sent more secure, because gmail is treating
them like potential fishing content.
models.py
from django.conf import settings
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
from django.contrib import messages
from django.core.mail import send_mail
from django.conf import settings
from django.core.validators import RegexValidator
from django.db import models
from django.db.models.signals import post_save
# Create your models here.
from .utils import code_generator
USERNAME_REGEX = '^[a-zA-Z0-9.+-]*$'
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
username = username,
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
username,
email,
password=password,
)
user.is_admin = True
user.is_staff = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
username = models.CharField(
max_length=255,
validators=[
RegexValidator(
regex = USERNAME_REGEX,
message = 'Username must be Alpahnumeric or contain any of the following: ". # + -" ',
code='invalid_username'
)],
unique=True,
)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
zipcode = models.CharField(max_length=120, default="92660")
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
# #property
# def is_staff(self):
# "Is the user a member of staff?"
# # Simplest possible answer: All admins are staff
# return self.is_admin
class ActivationProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
key = models.CharField(max_length=120)
expired = models.BooleanField(default=False)
def save(self, *args, **kwargs):
self.key = code_generator()
super(ActivationProfile, self).save(*args, **kwargs)
def post_save_activation_receiver(sender, instance, created, *args, **kwargs):
if created:
#send email
subject = 'Registration'
message = "http://127.0.0.1:8000/activate/{0}".format(instance.key)
from_email = settings.EMAIL_HOST_USER
recipient_list = ['UserEmail']
print(recipient_list)
send_mail(subject, message, from_email, recipient_list, fail_silently=True)
post_save.connect(post_save_activation_receiver, sender=ActivationProfile)
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
city = models.CharField(max_length=120, null=True, blank=True)
def __str__(self):
return str(self.user.username)
def __unicode__(self):
return str(self.user.username)
def post_save_user_model_receiver(sender, instance, created, *args, **kwargs):
if created:
try:
Profile.objects.create(user=instance)
ActivationProfile.objects.create(user=instance)
except:
pass
post_save.connect(post_save_user_model_receiver, sender=settings.AUTH_USER_MODEL)
forms.py
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth import authenticate, get_user_model
from django.core.validators import RegexValidator
from django.db.models import Q
from .models import USERNAME_REGEX
User = get_user_model()
class UserLoginForm(forms.Form):
query = forms.CharField(label='Username / Email')
password = forms.CharField(label='Password', widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
query = self.cleaned_data.get("query")
password = self.cleaned_data.get("password")
user_qs_final = User.objects.filter(
Q(username__iexact=query)|
Q(email__iexact=query)
).distinct()
if not user_qs_final.exists() and user_qs_final.count() != 1:
raise forms.ValidationError("Invalid credentials -- user not exist")
user_obj = user_qs_final.first()
if not user_obj.check_password(password):
# log auth tries
raise forms.ValidationError("Invalid credentials -- passowrd invalid")
if not user_obj.is_active:
raise forms.ValidationError("Inactive user. Please verify your email address.")
self.cleaned_data["user_obj"] = user_obj
return super(UserLoginForm, self).clean(*args, **kwargs)
class UserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'email',)
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.is_active = False
# create a new user hash for activating email.
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = ReadOnlyPasswordHashField()
class Meta:
model = User
fields = ('username', 'email', 'password', 'is_staff', 'is_active', 'is_admin')
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
views.py
from django.contrib.auth import login, get_user_model, logout
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render
# Create your views here.
User = get_user_model()
from .forms import UserCreationForm, UserLoginForm
from .models import ActivationProfile
def home(request):
if request.user.is_authenticated():
print(request.user.profile.city)
return render(request, "base.html", {})
def register(request, *args, **kwargs):
form = UserCreationForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect("/login")
return render(request, "accounts/register.html", {"form": form})
def login_view(request, *args, **kwargs):
form = UserLoginForm(request.POST or None)
if form.is_valid():
user_obj = form.cleaned_data.get('user_obj')
login(request, user_obj)
return HttpResponseRedirect("/")
return render(request, "accounts/login.html", {"form": form})
def logout_view(request):
logout(request)
return HttpResponseRedirect("/login")
def activate_user_view(request, code=None, *args, **kwargs):
if code:
act_profile_qs = ActivationProfile.objects.filter(key=code)
if act_profile_qs.exists() and act_profile_qs.count() == 1:
act_obj = act_profile_qs.first()
if not act_obj.expired:
user_obj = act_obj.user
user_obj.is_active = True
user_obj.save()
act_obj.expired = True
act_obj.save()
return HttpResponseRedirect("/login")
# invalid code
return HttpResponseRedirect("/login")
1) Replace recipient_list = ['UserEmail'] with recipient_list = [instance.user.email]
2) Sending mails is complicated.
No, I am lying, sending mails is simple, making them arrive in the inbox and not the spam is complicated.
There are multiple factors why this could happen but I would check the following:
Am I sending from the correct mail server - for example if you started a local mail server but the sender email is "foo#gmail.com" then to the receiving mail server this will look fishy as the sender is not gmail.com it is a bit more complicated but this is the short explanation
Also check the content that you are sending - mails containing spammy words (body parts enlargement etc.) could be filtered.
Here is my problem:
I'm tying to authenticate a custom AbstractBaseUser from shell:
>>from django.contrib import auth
>>u = auth.authenticate(username = 'test#mejl.com', password = '123')
>>u == None
True
>>from userprofiles.models import MyUser
>> MyUser(email = 'new#hope.com', password = '321', date_of_birth='1999-9-9').save()
>>u = auth.authenticate(username = 'new#hope.com', password = '321')
>>u == None
True
>>u = auth.authenticate(email= 'new#hope.com', password = '321') #just to check
>>u == None
True
Tried with users saved from form (beacause of pass hash), users inserted manually in db and users made and saved from shell.
Here is my User model class:
from django.db import models
from django.utils.crypto import get_random_string
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser)
import string
import random
class MyUserManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(email,
password=password,
date_of_birth=date_of_birth
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
role = models.CharField(max_length=15, default='guest')
confirmation_key = models.CharField(max_length=50,default = '/')
registration_confirmed = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
def is_guest(self):
if self.role == 'guest':
return True
return False
def is_restaurant_manager(self):
if self.role == 'restmanager':
return True
return False
def is_system_manager(self):
if self.role == 'sysmanager':
return True
return False
def get_role(self):
return self.role
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
#property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
def set_reg_key(self, key):
self.confirmation_key = key
Here is the view where i save it to db:
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
print "usao"
form.save()
print form.cleaned_data['email']
emailk = form.cleaned_data['email']
set_unique_reg_key(emailk)
return HttpResponseRedirect('/accounts/register1success')
else:
print "false je"
args = {}
args.update(csrf(request))
args['form']=UserCreationForm()
return render_to_response('userprofiles/register.html', args)
def set_unique_reg_key(emailk):
reg_key = ''.join(random.choice(string.ascii_uppercase) for i in range(12))
uu = MyUser.objects.filter(confirmation_key=reg_key)
if not uu:
try:
u = MyUser.objects.get(email=emailk)
except(KeyError, MyUser.DoesNotExist):
print
u.set_reg_key(reg_key)
print u.confirmation_key
u.save()
else:
set_unique_reg_key(emailk)
Here is form:
class UserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput,required=True)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput,required=True)
class Meta:
model = MyUser
fields = ('email', 'date_of_birth')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
Also, i have set:
AUTH_PROFILE_MODULE = 'userprofiles.MyUser'
And, if I understood corectly authenticate should work for any model made from AbstractBaseUser..so I haven't made CustomBackend.
I think you should use AUTH_USER_MODEL instead of AUTH_PROFILE_MODULE if you are using Django 1.7 or later, because it is removed according th this.
What is the name of your app?
The syntax of AUTH_USER_MODEL is app.model.
Your problem might be with the authenticate function. Here's my function from my own custom model.
def authenticate(self, username=None, password=None):
""" Authenticate a user based on email address as the user name. """
try:
user = UserAccount.objects.get(email=username)
if user.check_password(password):
return user
except UserAccount.DoesNotExist:
return None
Then again, you're sending the email as the username to authenticate...
I have a user in my database with the login jim#test.com and password jimrox. I'm trying to log him in with this view:
def login(request):
email = request.POST.get("email", "")
password = request.POST.get("password", "")
user = authenticate(username=email, password=password)
My custom authentication looks partly like this:
class login(object):
def authenticate(self, username=None, password=None):
# auth user based on email
try:
user = Freelancer.objects.get(email=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
When the user attempts to check_password() it doesn't return the user even though the password is correct. Am I meant to create my own check_password() function in the model?
Here is my model also:
class FreelancerManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
class Freelancer(AbstractBaseUser):
email = models.EmailField()
first_name = models.CharField(max_length=128, primary_key=True)
surname = models.CharField(max_length=128)
university = models.CharField(max_length=256)
verified = models.BooleanField(default=False)
created_date = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
If I am meant to create my own password check, how would I do that? This is with 1.8 also.
EDIT
Here is how I add my users to the database:
views.py
def freelancer_signup(request):
if request.method == 'POST':
form = FreelancerForm(request.POST)
if form.is_valid():
freelancer = form.save(commit=False)
freelancer.save()
return render(request, 'freelancestudent/index.html')
else:
return render(request, 'freelancestudent/index.html')
else:
form = FreelancerForm()
return render(request, 'freelancestudent/freelancersignup.html', {'form': form})
forms.py
from django import forms
from models import Freelancer
class FreelancerForm(forms.ModelForm):
class Meta:
model = Freelancer
password = forms.CharField(widget=forms.PasswordInput)
fields = ('email', 'first_name', 'surname', 'university', 'password')