Reputation: 3
I'm doing a registration for a user using jquery for the event of the register button, although my createUser method correctly registers a user does, not redirect to the indicated page, but paints it by console
views.py
@csrf_exempt
def createUser(request):
#if request.method == 'POST':
''' nombres = request.POST.get('nombres')
apellidos = request.POST.get('apellidos')
email = request.POST.get('email')
password = request.POST.get('password')
direccion = request.POST.get('direccion')
hour = timezone.now()
day = timezone.now()
myuser=User(password,day,hour,email,nombres,apellidos,direccion)
myuser.save()
'''
return redirect('http://127.0.0.1:8000/platos/')
def platos(request):
platos=Plato.objects.all()
return render(request,"core/platos.html",{'platos':platos})
urls.py
path('register/',views.createUser,name="register"),
path('platos/',views.platos,name="platos"),
jquery
$('#registro').click(function(){
var nombres = $("#exampleInputNombresRegistrarse").val();
var apellidos = $("#exampleInputApellidosRegistrarse").val();
var email = $("#exampleInputEmailRegistrarse").val();
var password = $("#exampleInputPasswordRegistrarse").val();
var direccion=$("#exampleInputDireccionRegistrarse").val();
if (nombres == '' || email == '' || password == '' || apellidos == ''
|| direccion == '') {
alert("Por favor completa todos los campos...!!!!!!");
}
else if(email.indexOf('@', 0) == -1 || email.indexOf('.', 0) == -1){
alert("Por favor ingrese un correo válido...!!!!!!");
}
else{
alert("Bien hecho "+nombres);
$.ajax({
url: "http://127.0.0.1:8000/register/",
method: 'POST', // or another (GET), whatever you need
data: {'nombres': nombres,'apellidos':apellidos,'email':email,
'password':password,'direccion':direccion
},
success: function (data) {
// success callback
// you can process data returned by function from views.py
console.log(data);
}
});
}
});
Upvotes: 0
Views: 118
Reputation: 662
You can write window.location.href = 'http://127.0.0.1:8000/platos/';
inside success function of your ajax call. It'll redirect you to http://127.0.0.1:8000/platos/
Upvotes: 1
Reputation: 984
You can do this, in view.py
return redirect('/platos/')
Or use reverse-resolve the name technique
return redirect(reverse('platos'))
"platos" is just reverse relationship name in that url
for more details about redirect
Upvotes: 0