Django Check if Username already exists - python

Here's my forms.py,
class RegistrationForm(UserCreationForm):
class Meta:
model = User
fields = [ 'username', 'first_name', 'password1', 'password2']
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
if commit:
user.save()
return user
In views.py,
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
username = request.POST.get('username')
password = request.POST.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect(reverse('accounts:profile'))
else:
form = RegistrationForm()
return render(request, 'accounts/reg_form.html', {'form': form})
Right now if i'm using the same username it's raising an error which says "The view accounts.views.register didn't return an HttpResponse object. It returned None instead." How can I fix this issue?
Thank You :)

Django form by default does this for you.
You don't need specific for this.
Beacuse, default User Model provided by django doesn't take duplicate username.
May be some indentation problem
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
. . . .
else:
form = RegistrationForm()
return render(request, 'accounts/reg_form.html', {'form': form})
#^^^^^Indentation here take return statement outside else

Related

Adding a profile to user in django

Following this :
https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#onetoone
I am having some trouble with this call:
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
In their example I am guessing this works as is on the signup of a new account because the Profile in their example has all fields that can be blank and null. In my case my profile I am trying to maintain here is called:
class APOUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
institution = models.ForeignKey("mainpage.InstitutionMember", on_delete=models.PROTECT)
gender = models.ForeignKey("mainpage.GenderTable", on_delete=models.PROTECT)
on_site_status = models.ForeignKey("mainpage.SiteStatus", on_delete=models.PROTECT)
refer_to_as = models.TextField(max_length = 30, blank=True) #if the above is custom
room_preference = models.ForeignKey("housing.Room", on_delete=models.PROTECT)
Which has references to ultimately what will be drop downs to select form a form (populated by another table with defaults). So do I remove the #reciever and then just have the users fill out the profile separately somehow after they signup and confirm their account?
I tried to mix my signup form with the profile form... but kept getting an anonmyous user object had no attribute object apouser in the views.py when I try to mix the signup forms and profile forms:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
profile_form = ProfileForm(request.POST, instance=request.user.apouser)
if form.is_valid() and profile_form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
profile_form.save()
current_site = get_current_site(request)
mail_subject = 'Activate your APO account.'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return redirect('plsactivate')
#return HttpResponse('Please confirm your email address to complete the registration')
#form.save()
#username = form.cleaned_data.get('username')
#raw_password = form.cleaned_data.get('password1')
#user = authenticate(username=username, password=raw_password)
#login(request, user)
#return redirect('/')
else:
form = SignUpForm()
profile_form = ProfileForm(instance=request.user.apouser) #<-- error here
return render(request, 'signup.html', {'form': form, 'profile_form': profile_form})
#return render(request, 'signup.html', {'form': form})
So not sure the proper way to go about what I need. I have my regular user default with the basic stuff the default django signup comes up with. THen I have the APOUser with extra stuff (usually filled out in a profile) but not sure where/how to get users to fill that out.
On signup gives me the anonymous user error
After signup doesn't let the #reciever work because none of the APOUuser stuff is filled out?
Yes, remove the receiver - although it's not the cause of this specific problem, it will cause issues as soon as you fix that
The reason for your error is as it says: since you don't have a logged in user yet, you don't have a profile either. But you don't need one; there is no need to pass an instance argument there.
What you do need to do is to set the user on the result of saving the profile form - in just the she way as you set is_active on the result of saving the user form.
So:
if request.method == 'POST':
form = SignUpForm(request.POST)
profile_form = ProfileForm(request.POST,)
if form.is_valid() and profile_form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
...
else:
form = SignUpForm()
profile_form = ProfileForm()
...

'User' object has no attribute 'ValidationError'

I'm trying to create a registration form. Everything works fine until I try to raise a validation error in case email already exists. It says, 'User' object has no attribute 'ValidationError'
Here's what I did,
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
new_form = form.save(commit=False)
mail = form.cleaned_data['email']
if User.objects.filter(email=mail).exists():
raise new_form.ValidationError('Looks like email already exists')
else:
new_form.save()
username = request.POST.get('username')
password = request.POST.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect(reverse('accounts:profile'))
else:
form = RegistrationForm()
How can we fix that?
When validating a form error, you should import validationError from django.forms and not django.core.exceptions
try :
from django.forms import ValidationError
try to add below method to your form
from django import forms
from .models import User
class RegistrationForm(forms.Form):
# ..............
# ..............
def clean(self):
mail = form.cleaned_data['email']
if User.objects.filter(email=mail).exists():
raise forms.ValidationError('Looks like email already exists')
return self.cleaned_data
change the code in views.py
if User.objects.filter(email=mail).exists():
raise new_form.ValidationError('Looks like email already exists')
else:
new_form.save()
to
new_form.save()
You should raise ValidationError like this:
raise ValidationError('Looks like email already exists')
Here is the full code:
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
new_form = form.save(commit=False)
mail = form.cleaned_data['email']
if User.objects.filter(email=mail).exists():
raise ValidationError('Looks like email already exists')
else:
new_form.save()
username = request.POST.get('username')
password = request.POST.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect(reverse('accounts:profile'))
else:
form = RegistrationForm()
if User.objects.filter(email=mail).exists():
raise new_form.ValidationError('Looks like email already exists')
use "forms" instead of "new_form" the result should look like
if User.objects.filter(email=mail).exists():
raise forms.ValidationError('Looks like email already exists')
I typed as follows and it worked at django-2.0.7:
from django import forms
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
class RegistrationForm(forms.ModelForm):
def clean_email(self):
email = self.cleaned_data['email']
if self.Meta.model.objects.filter(email=email).exists():
raise forms.ValidationError('Looks like email already exists')
return email
class Meta:
model = User
fields = ('username', 'password', 'email')
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.set_password(user.password)
user.save()
login(request, user)
return redirect('accounts:profile')
else:
form = RegistrationForm()
return render(request, 'register.html', {'form': form})

Adding to userform in django before saving in database

Im sure this was working and now it's not. Anyone know what could be up? its failing at the new_user = form.save(commit=False)
class Signup(View):
def get(self, request):
return render(request, 'accounts/signup.html')
def post(self, request):
form = UserCreationForm(request.POST)
new_user = form.save(commit=False)
email=new_user.cleaned_data.get('email')
new_user.username=email
if new_user.is_valid():
new_user.save()
username = new_user.cleaned_data.get('username')
raw_password = new_user.cleaned_data.get('password')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('/accounts/home/')
That line and the following two should be inside the if block; you can't save if the form is not valid.

How to login either through a mobile no or a email address in django?

I initially used to login using email and password but now want to change it in such a way that we can login using either mobile no or email address and password.I am not able to understand what parts to change in my code as I do not want to delete already existing user and data.
forms.py
from django.contrib.auth.models import User
from django import forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['first_name', 'last_name', 'email', 'password']
views.py
class UserFormView(View):
form_class = UserForm
template_name = 'main/registration_form.html'
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned (normalized) data
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user.username = email
user.set_password(password)
user.save()
# return user objects if credentials are correct
user = authenticate(username=email, password=password)
if user is not None:
if user.is_active:
login(request, user)
# request.user.username display their username
return redirect('main:register2')
return render(request, self.template_name, {'form': form})
class LoginForm(View):
form_class = UserForm
template_name = 'main/login_form.html'
def get(self, request):
form = self.form_class(None)
if error:
return render(request, self.template_name, {'form': form},
{'error': error})
else:
return render(request, self.template_name, {'form': form})
def post(self, request):
email = request.POST.get('email', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=email, password=password)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
auth.login(request, user)
# Redirect to a success page.
return HttpResponseRedirect("/main/home/")
else:
# Show an error page
error = True
return HttpResponseRedirect("/main/", {'error': error})
Please answer considering the fact that I am new to Django. Thanks in advance :)
In the post method of your view, identify what has the user given you. If it's an email, feed the Django's authenticate() method with the email. If not, then assume it's a phone number (you may also run additional validations in there) and use this in the authentication procedure.
Another approach would be to create a custom authenticate() method, which would look like this:
from django.contrib.auth.backends import ModelBackend
class MobilePhoneOrEmailModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
# the username could be either one of the two
if '#' in username:
kwargs = {'email': username}
else:
kwargs = {'mobile_phone': username}
try:
user = User.objects.get(**kwargs)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, username):
try:
return User.objects.get(pk=username)
except User.DoesNotExist:
return None
Let me know if this worked out well for you.
Credits to user user3282276 in this thread as well: Log in user using either email address or username in Django

Display django user creation form error

I'm using django built in user creation form which I extended like this
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'E-mail address'}))
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
def clean_email(self):
email = self.cleaned_data["email"]
try:
User._default_manager.get(email=email)
except User.DoesNotExist:
return email
#raise forms.ValidationError(self.error_messages['duplicate_email'])
raise forms.ValidationError('duplicate_email')
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
As you see I've added email validation to check if there is already user with that email (what I don't accept).
The problem is that neither that of those errors are displaying in my template and I don't know why. I've tried with {{form.field.errors}} and even with {{form.errors.values.[i]}} but nothing helps
I looked in auth.forms to check class UserCreationForm and I see that it pass errors to error_messages but I couldn't find the way to display them.
Although all validations (username check, password check, email check) work fine I'd still like to display error above field with error so that user understands why he couldn't create his account.
UPDATE
views.py
def register_user(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
args = {}
args.update(csrf(request))
args['form'] = RegistrationForm()
return render_to_response('user_profile/register.html', args, context_instance=RequestContext(request))
I hope somebody can help me with that.
Thanks
This will work:
def register_user(request):
args = {}
args.update(csrf(request))
if request.method == 'POST':
form = RegistrationForm(request.POST)
args['form'] = form
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
else:
args['form'] = RegistrationForm()
return render_to_response('user_profile/register.html', args, context_instance=RequestContext(request))
The problem with your code was that you was passing an unbound form instance to your template all the time, so you was just overwriting any occured errors.
Look here for a reference: docs
If you are looking at more efficient solution then you should try my below code in views.py file.
Your forms.py code will be same. But it is leakage of validation of password.
# to register auth user
class register_user(View):
template1 = "app_name/register.html" # define templates
template2 = "app_name/login.html"
def get(self, request): # get method
form = RegistrationForm()
return render(request, self.template1, locals())
def post(self, request): # post method
form = RegistrationForm(request.POST) # create form object
if form.is_valid():
print "Form is validated"
user_data = form.save(commit=False)
user_data.save()
return render(request, self.template2, locals())
else:
print "with error"
return render(request, self.template1, locals())
I hope this will help. I'm still working on good practice of Django.

Categories

Resources