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
)
Related
I am trying to register new users in my django ecommerce webapplication. I am using custombased model. But whenever i submit the form, my database shows that the user has been saved but then this error "duplicate key value violates unique constraint "account_account_email_key"
DETAIL: Key (email)=(macmusonda#gmail.com) already exists." still comes up.The user can even login..How do i get rid of this error??
My views.py
import email
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from account.forms import RegistrationForm
from django.contrib import messages
from .models import Account
def registration(request):
if request.method == "POST":
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
first_name = request.POST['first_name']
other_names = request.POST['other_names']
email = request.POST['email']
username = request.POST['username']
raw_password = request.POST['password1']
phonenumber = request.POST.get('phonenumber')
Address_1 = request.POST['Address_1']
Address_2 = request.POST['Address_2']
user = Account.objects.create_user(first_name=first_name, other_names=other_names, email=email, username=username, password=raw_password, phonenumber=phonenumber, Address_1=Address_1, Address_2=Address_2)
user.save()
messages.success(request, ('Your account was successfully created!'))
login(request, user)
return redirect('home')
else:
messages.error(request, ('Invalid input.'))
else:
form = RegistrationForm()
return render(request, 'account/register.html', {'form': form})
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
try:
user = Account.objects.get(username=username)
except:
messages.error(request, 'username does not exist')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.error(request, 'Invalid Username Or Password')
return render(request, 'account/login.html')
And my models.py
import email
from django.db import models
from django.conf import settings
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.base_user import BaseUserManager
class MyAccountManager(BaseUserManager):
def _create_user(self, email, username, password, first_name, other_names, phonenumber, Address_1, Address_2):
if not email:
raise ValueError("User must have an email")
if not username:
raise ValueError("User must have a username")
user = self.model(
email = self.normalize_email(email),
username = username, password=password, first_name = first_name, other_names = other_names,
phonenumber = phonenumber, Address_1 = Address_1, Address_2 = Address_2
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, username, password, first_name=None, other_names=None, phonenumber=None, Address_1=None, Address_2=None):
return self._create_user(email, username, password,first_name, other_names, phonenumber, Address_1, Address_2)
def create_superuser(self, email, username, password):
"""
Creates and saves a superuser with the given email, name and password.
"""
user = self.create_user(email=email,
username=username,
password=password,
)
user.is_admin = True
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(verbose_name='first_name', max_length=60, null=True)
other_names = models.CharField(verbose_name='other_names', max_length=200, null=True)
phonenumber = PhoneNumberField(verbose_name='phonenumber', null=True)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=60, unique=True)
Address_1 = models.CharField(max_length=60, blank=False, null=True)
Address_2 = models.CharField(max_length=60, blank=False, 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)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = MyAccountManager()
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
I really dont know how to go about this error..need some explanation on where its coming from and how to get rid of it
Hi I wanted to create user accounts from django admin pannel and then use the email and passsword stored in the model to login?
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['email']
password = request.POST['password']
user = authenticate(email=email, password=password)
if user:
login(request, user)
return redirect("home")
else:
form = AccountAuthenticationForm()
context['login_form'] = form
return render(request, 'login.html', context)
forms.py
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['email']
password = self.cleaned_data['password']
if not authenticate(email=email, password=password):
raise forms.ValidationError("Invalid login")
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError("Users must have an email address")
user = self.model(
email=self.normalize_email(email))
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
date_joined=models.DateTimeField(verbose_name='datejoined',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)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
Please ignore the indentation, it is bit messed up as I am new to stack overflow and uploaded code for the first time.
Any sort of help would be appreciated.
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.
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')
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.