0

How do I insert an id inside a url template tag using javascript? I tried this way and I did not get it. Can not find the route in the urls.py.

function Edit(pk){
    window.location.assign("{% url 'authentication:edit_user' "+${pk}+" %}");
}
<!-- Other way -->
function Edit(pk){
    window.location.assign("{% url 'authentication:edit_user' "+pk+" %}");
}

Error is:

Reverse for 'edit_user' with arguments '('+${pk}+',)' not found. 1 pattern(s) tried: ['authentication\/user\/edit\/(?P[0-9]+)\/$']

2 Answers 2

1

This can't possibly work. Template tags are evaluated server-side, well before the client-side Javascript can run.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to concatinate the string. The url tag takes parameter. So you can change your code to

window.location.assign("{% url 'authentication:edit_user' pk %}");

Reference - url tag

2 Comments

Thanks for answering. If I do not concatenate this error appears and not find the route. Reverse for 'edit_user' with arguments '('',)' not found. 1 pattern(s) tried: ['authentication\\/user\\/edit\\/(?P<pk>[0-9]+)\\/$']
@DevPython, That error indicates the pk field is empty. You may not be passing it to template properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.