Django – call url from JavaScript with dynamic parameters

I recently had to use JavaScript to call a URL from Django with dynamic parameter. Here’s what I found that did what I wanted:

function validateDeleteTenant(tenant_pk) {
	submitted = confirm("This would physically DELETE the tenant and all data from the server. Are you sure you want to continue? \n\nPlease note that a log will be submitted to provisioning team for auditing purposes.");
	if(submitted) {
		window.location='{% url 'deleteTenantFromServer' 0 %}'.replace(/0/, tenant_pk);
	}
}

Where, the deleteTenantFromServer URL looks like this:

url(r'^tenant/deleteFromServer/(?P\d+)/$', views.deleteTenantFromServer, name='deleteTenantFromServer'),
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s