def registro(request, template_name="programacao/registro.html"): if request.method == "POST": aluno_form = AlunoForm(request.POST or None) user_form = RegistroForm(request.POST or None) if aluno_form.is_valid() and user_form.is_valid(): with transaction.atomic(): user = user_form.save(commit=False) user.is_active = True user.save() aluno = aluno_form.save(commit=False) aluno.user = user aluno.save() permission = Permission.objects.get(name="view_aluno") user.user_permissions.add(permission) messages.add_message(request, messages.INFO, "Cadastro realizado com sucesso!") return redirect("programacao:index") else: messages.add_message(request, messages.ERROR, "Erro ao tentar cadastrar!") aluno_form = AlunoForm() user_form = RegistroForm() data = {} data["aluno_form"] = aluno_form data["user_form"] = user_form return render(request, template_name, data)
def aluno_edit(request, template_name="programacao/aluno_edit.html"): user = request.user aluno = Aluno.objects.get(user_id=user.id) user_form = UserEditForm(request.POST or None, instance=user) aluno_form = AlunoForm(request.POST or None, instance=aluno) if aluno_form.is_valid() and user_form.is_valid(): user_form.save() aluno_form.save() return redirect("programacao:aluno_edit") data = {} data["user_form"] = user_form data["aluno_form"] = aluno_form return render(request, template_name, data)