问题
Here I am using a non custom user model for saving the users registered and their details are stored in user_requirement model now I want to validate before logging in to the dashboard My models.py:
class user(models.Model):
username=models.CharField(max_length=20)
email=models.CharField(max_length=50,unique=True)
password=models.CharField(max_length=50,default='0000000')
created_at = models.DateTimeField(default=datetime.now)
updated_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.username
class User_Requirement(models.Model):
user=models.ForeignKey(user,on_delete=models.CASCADE)
room = models.ForeignKey(room,on_delete=models.CASCADE)
goal = models.ManyToManyField(goal)
design = models.ManyToManyField(design)
furniture = models.ForeignKey(furniture,on_delete=models.CASCADE)
created_at = models.DateTimeField(default=datetime.now)
updated_at = models.DateTimeField(default=datetime.now)
My forms.py:
class UserForm(forms.ModelForm):
class Meta:
model = user
fields = ('username','email','password')
def clean_email(self):
# Get the email
email = self.cleaned_data.get('email')
# Check to see if any users already exist with this email as a username.
try:
match = User.objects.get(email=email)
except User.DoesNotExist:
# Unable to find a user, this is fine
return email
raise forms.ValidationError('This email address is already in use.')
class UserRequirementForm(forms.ModelForm):
class Meta:
model = User_Requirement
fields=('room','goal','design','furniture')
error_messages = {
'room': {
'required': "Please select any of the room in the first step",
},
'goal': {
'required': "Please select any of the goals in the second step",
},
'design': {
'required': "Please select any of the design in the forth step",
},
'furniture': {
'required': "Please select any of the furniture in the third step",
},
}
My views.py:
def user_register(request):
if request.method == 'POST': # if there is a post request in the form
user_form = UserForm(data=request.POST) #first of all it is a user_form will be posted details present in the user_form
user_requirement_form = UserRequirementForm(data=request.POST)# after posting the details of the user_form post the details
if user_form.is_valid() and user_requirement_form.is_valid(): # if user_form & user_requirement form is valid
user = user_form.save()#if form is valid save
user_requirement = user_requirement_form.save(commit=False)
# Set user
user_requirement.user = user
user_requirement.save()
user_requirement_form.save_m2m()
messages.success(request,('Project saved successfully'))
return render(request,'home1.html')
else:
messages.warning(request, 'Please correct the errors above')
else:
user_form = UserForm()
user_requirement_form = UserRequirementForm()
return render(request,'register.html', {'user_form': user_form, 'requirements_form': user_requirement_form})
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
messages.success(request, 'You are now logged in')
return redirect('dashboard')
else:
messages.error(request, 'Invalid credentials')
return render(request,'login.html')
else:
return render(request, 'login.html')
Here registration is being done perfectly but the registered users should be able login right but the view which I wrote is not validating please see and say If I did any wrong
来源:https://stackoverflow.com/questions/59730564/how-to-validate-the-registered-users-with-login-in-django