[Fixed]-Django Is there a way to get a reversed url without passing parameters?

0👍

After thinking about it, and looking at comments, I decided on the answer of adding a fake parameter and removing it afterwards. Since it will be removed when the form is rendered, the parameter doesn’t much matter. I chose 1.

So, in my template, I put {% url 'jobs:close' '1' %}. Then in my javascript, as I call the form in a modal, I use jquery to find the form. It makes it easy just to use the same URL again, so here it is:

var modal = $("#modal");
var closeUrl =  '{% url 'jobs:close' '1' %}';
closeUrl = closeUrl.substring(0, closeUrl.length - 2) + event.data.id + /;
modal.find("#close-job").attr('action', closeUrl);

event.data.id is where I am storing the job ID that I really need.

It isn’t as slick as I was looking for, but it works well.

👤PoDuck

1👍

Instead of getting ‘foo’ in ajax response, get the reverse url in ajax response and replace the DOM where you need the url.

Leave a comment