Re: JsonResponse and Ajax function.

2015-02-19 Thread Collin Anderson
Hi,

Sorry for the late reply. Do you need to use JSON.parse(response).status?

Collin

On Tuesday, February 10, 2015 at 8:33:58 AM UTC-5, elcaiaimar wrote:
>
> Hello!
>
> I've written a code to delete db registers and I want to call an ajax 
> function when the delete is done.
> At the moment, when I click my delete button I only receive a message: 
> {"status":"True","product_id":p.id} or {"status":"False"} 
> and this should be sent to the ajax function and I shoud have an alert 
> saying to me: product has been removed.
> I don't know why, probably there is something wrong in the relation of my 
> JsonResponse and ajax function.
>
> I put my code below. Does anybody know how can I fix this?
> Thank you very much!
>
> *template*:
>
>  
>   {% csrf_token %}
>   
>aria-hidden="true">Cerrar button>
>   Eliminar
>  
>
> *views.py:*
>
> if request.method=="POST":
> if "product_id" in request.POST:
> try:
> id_producto = request.POST['product_id']
> p = Pozo.objects.get(pk=id_producto)
> mensaje = {"status":"True","product_id":p.id}
> p.delete() # Elinamos objeto de la base de datos
> return JsonResponse(mensaje)
> except:
> mensaje = {"status":"False"}
> return JsonResponse(mensaje)
>
>
>
> *Ajax.js:*// Autor: @jqcaper
> // Configuraciones Generales
> var nombre_tabla = "#tabla_productos"; // id
> var nombre_boton_eliminar = ".delete"; // Clase
> var nombre_formulario_modal = "#frmEliminar"; //id
> var nombre_ventana_modal = "#myModal"; // id
> // Fin de configuraciones
> $(document).on('ready',function(){
> $(nombre_boton_eliminar).on('click',function(e){
> e.preventDefault();
> var Pid = $(this).attr('id');
> var name = $(this).data('name');
> $('#modal_idProducto').val(Pid);
> $('#modal_name').text(name);
> });
> var options = {
> success:function(response)
> {
> if(response.status=="True"){
> alert("Eliminado!");
> var idProd = response.product_id;
> var elementos= $(nombre_tabla+' >tbody >tr').length;
> if(elementos==1){
> location.reload();
> }else{
> $('#tr'+idProd).remove();
> $(nombre_ventana_modal).modal('hide');
> }
> }else{
> alert("Hubo un error al eliminar!");
> $(nombre_ventana_modal).modal('hide');
> };
> }
> };
> $(nombre_formulario_modal).ajaxForm(options);
> });
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a798d66d-b9eb-4698-98fe-8944362b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


JsonResponse and Ajax function.

2015-02-10 Thread elcaiaimar
Hello!

I've written a code to delete db registers and I want to call an ajax 
function when the delete is done.
At the moment, when I click my delete button I only receive a message: 
{"status":"True","product_id":p.id} or {"status":"False"} 
and this should be sent to the ajax function and I shoud have an alert 
saying to me: product has been removed.
I don't know why, probably there is something wrong in the relation of my 
JsonResponse and ajax function.

I put my code below. Does anybody know how can I fix this?
Thank you very much!

*template*:

 
  {% csrf_token %}
  
  Cerrar
  Eliminar
 

*views.py:*

if request.method=="POST":
if "product_id" in request.POST:
try:
id_producto = request.POST['product_id']
p = Pozo.objects.get(pk=id_producto)
mensaje = {"status":"True","product_id":p.id}
p.delete() # Elinamos objeto de la base de datos
return JsonResponse(mensaje)
except:
mensaje = {"status":"False"}
return JsonResponse(mensaje)



*Ajax.js:*// Autor: @jqcaper
// Configuraciones Generales
var nombre_tabla = "#tabla_productos"; // id
var nombre_boton_eliminar = ".delete"; // Clase
var nombre_formulario_modal = "#frmEliminar"; //id
var nombre_ventana_modal = "#myModal"; // id
// Fin de configuraciones
$(document).on('ready',function(){
$(nombre_boton_eliminar).on('click',function(e){
e.preventDefault();
var Pid = $(this).attr('id');
var name = $(this).data('name');
$('#modal_idProducto').val(Pid);
$('#modal_name').text(name);
});
var options = {
success:function(response)
{
if(response.status=="True"){
alert("Eliminado!");
var idProd = response.product_id;
var elementos= $(nombre_tabla+' >tbody >tr').length;
if(elementos==1){
location.reload();
}else{
$('#tr'+idProd).remove();
$(nombre_ventana_modal).modal('hide');
}
}else{
alert("Hubo un error al eliminar!");
$(nombre_ventana_modal).modal('hide');
};
}
};
$(nombre_formulario_modal).ajaxForm(options);
});

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/66cfb1b0-e5e0-4393-8e87-2cde5a9f77c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


JsonResponse and ajax function

2015-02-10 Thread elcaiaimar
Hello!

I've written a code to delete db registers and I want to call an ajax 
function when the delete is done.
At the moment, when I click my delete button I only receive a message: 
{"status":"True","product_id":p.id} or {"status":"False"} 
and this should be sent to the ajax function and I shoud have an alert 
saying to me: product has been removed.
I don't know why, probably there is something wrong in the relation of my 
JsonResponse and ajax function.

I put my code below. Does anybody know how can I fix this?
Thank you very much!

*template*:

 
  {% csrf_token %}
  
  Cerrar
  Eliminar
 

*views.py:*

if request.method=="POST":
if "product_id" in request.POST:
try:
id_producto = request.POST['product_id']
p = Pozo.objects.get(pk=id_producto)
mensaje = {"status":"True","product_id":p.id}
p.delete() # Elinamos objeto de la base de datos
return JsonResponse(mensaje)
except:
mensaje = {"status":"False"}
return JsonResponse(mensaje)



*Ajax.js:*/ Autor: @jqcaper
// Configuraciones Generales
var nombre_tabla = "#tabla_productos"; // id
var nombre_boton_eliminar = ".delete"; // Clase
var nombre_formulario_modal = "#frmEliminar"; //id
var nombre_ventana_modal = "#myModal"; // id
// Fin de configuraciones
$(document).on('ready',function(){
$(nombre_boton_eliminar).on('click',function(e){
e.preventDefault();
var Pid = $(this).attr('id');
var name = $(this).data('name');
$('#modal_idProducto').val(Pid);
$('#modal_name').text(name);
});
var options = {
success:function(response)
{
if(response.status=="True"){
alert("Eliminado!");
var idProd = response.product_id;
var elementos= $(nombre_tabla+' >tbody >tr').length;
if(elementos==1){
location.reload();
}else{
$('#tr'+idProd).remove();
$(nombre_ventana_modal).modal('hide');
}
}else{
alert("Hubo un error al eliminar!");
$(nombre_ventana_modal).modal('hide');
};
}
};
$(nombre_formulario_modal).ajaxForm(options);
});

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c295d8a5-79bb-427b-be04-86dcb0d698e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.