简体   繁体   中英

django Is there a way to get a reversed url without passing parameters?

I am using ajax data to pass info to my page, but I need to create some links that use data from the javascript to call a view, so the parameters usually passed when one does a {% url 'jobs:close' foo %} , for example, are not available at the time the template is rendered, but imported later through an ajax call. Obviously, this causes errors when the template is rendered.

Is there a way to keep the benefit of having reverse url lookups in such a situation, and dynamically get the URL in the template without passing foo , or do I need to hard code the URL in the template and paste the parameter on the end later?

不要在ajax响应中获取“ foo”,而是在ajax响应中获取反向URL,并在需要URL的地方替换DOM。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM