Skip to content

Commit

Permalink
Persiste novo usuário no Banco
Browse files Browse the repository at this point in the history
  • Loading branch information
brnocesar committed Jan 20, 2021
1 parent c5b1601 commit a663965
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions apps/usuarios/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import render, redirect
from django.contrib.auth.models import User

def login(request):
return render(request, 'usuarios/login.html')
Expand All @@ -8,12 +9,34 @@ def logout(request):

def cadastro(request):
if request.method == 'POST':
nome = request.POST['nome']
email = request.POST['email']
password = request.POST['password']
nome = request.POST['nome']
email = request.POST['email']
password = request.POST['password']
password_confirmation = request.POST['password_confirmation']
print(nome, email, password, password_confirmation)
print(request.POST)

erros = 0
if not nome.strip():
print('=> Campo nome é obrigratório!')
erros += 1
if not email.strip():
print('=> Campo email é obrigratório!')
erros += 1
if not password.strip():
print('=> Campo senha é obrigratório!')
erros += 1
if password != password_confirmation:
print('=> As senhas devem ser iguais!')
erros += 1
if User.objects.filter(email=email).exists():
print('=> Usuário já é cadastrado!')
erros += 1
if erros > 0:
return redirect('cadastro')

user = User.objects.create_user(username=nome, email=email, password=password)
user.save()
print('=> Usuário cadastrado com sucesso!')

return redirect('login')

return render(request, 'usuarios/cadastro.html')
Expand Down

0 comments on commit a663965

Please sign in to comment.