49

It is known that there's a DRY way of directing to an URL by using django template tag "url", say

{% url "someview" arg1=X %}

Here, I want "X" to be the value of a javascript variable, say tmp. But the following doesn't work

<script>
    ...{% url "someview" arg1=tmp %}...
</script>

How should I get the value inside a template tag?

Mike Lee
  • 1,859
  • 2
  • 14
  • 15

6 Answers6

95

I found a trick that might work under most circumstances:

var url_mask = "{% url 'someview' arg1=12345 %}".replace(/12345/, tmp.toString());

It's clean, and it doesn't break DRY principle.

Mike Lee
  • 1,859
  • 2
  • 14
  • 15
  • 1
    Neat idea - worth noting that this'll only work if `arg1` can be numeric, and if the reversed URL doesn't contain other instances of the string "/12345/". – Dominic Rodger Jul 24 '13 at 13:02
  • 1
    @DominicRodger the value 12345 is just a placeholder of sorts, if the reversed URL did contain that placeholder, it would suffice to change it for something that isn't there. Why wouldn't it work if arg1 can't be numeric? – gdvalderrama Feb 06 '17 at 16:52
  • I have tried this but nothing is working in my case, it recognizes url before replacement taken place. "var url_mask = "{% url 'someview' pk=12345 %}".replace(/12345/, "1");" – jax Jul 21 '17 at 07:06
  • I have tried with App name and using django 1.11, its not working, var href = "{% url 'Myaap:report_page' args1=0 %}".replace(/0/, temp_location.toString()) – Vinay Kumar Jun 21 '19 at 09:44
  • I did like this & it worked for me, `var url_mask = "{% url 'someview' 12345 %}".replace(/12345/, tmp.toString());` – Gokul nath Aug 10 '20 at 03:25
18

Just making it clear so next readers can have a comprehensive solution. In regard to the question, this is how I used Mike Lee solution for an example case which is redirecting with Javascript to a new page with a parameter in a variable named tmp:

window.location = "{% url 'your_view_name' 1234 %}".replace(/1234/, tmp.toString());
Olfredos6
  • 808
  • 10
  • 19
2

one easy way to do this is

<div id="test" style="display: none;">
 {% url 'some_url:some_sub_url'%}
</div>


<script>
  url_mask = $( "#test" ).html()
</script>
Moayyad Yaghi
  • 3,622
  • 13
  • 51
  • 67
2
var name = "{% url 'your_view_name' pk=0 %}".replace('0', name_variable);
Zoe
  • 27,060
  • 21
  • 118
  • 148
Victor Tax
  • 21
  • 1
1

Similar solution with concat instead of replace, so you avoid the id clash:

var pre_url = "{% url 'topology_json' %}";
var url = pre_url.concat(tool_pk, '/', graph_depth, '/');

But you need a non-parameter url to keep Django pleasant with the pre_url because the loading-time evaluation, my urls.py is:

urlpatterns = [
    url(r'^topology_json/$',
        views.topology_json,
        name='topology_json'),
    url(r'^topology_json/(?P<tool_id>[0-9]+)/(?P<depth>[0-9]+)/$',
        views.topology_json,
        name='topology_json'),
    ...
]

And parameters in view have suitable default values, so views.py:

def topology_json(request, tool_id=None, depth=1):                                             
    """                                                                                        
    JSON view returns the graph in JSON format.                
    """                                                                                        
    if tool_id:                                                                                
        tool = get_object_or_404(Tool, pk=tool_id)                                             
        topology = get_graph(str(tool), int(depth))                                            
    else:
        ...      

Django yet 1.11 here.

santiagopim
  • 556
  • 5
  • 13
0

I don't think you'll be able to use a named argument to {% url %} from JavaScript, however you could do a positional argument or querystring parameter:

<script type="text/javascript">
    var tmp = window.location.hash, //for example
        url = {% url '[url_name]' %} + tmp + '/';
</script>

But, you could only do that once, when the template is initially rendered.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • Yeah, I come up with a similar trick. Something like var url_mask = "{% url 'someview' arg1=12345 %}".replace(/12345/, tmp.toString()); – Mike Lee Jul 24 '13 at 11:24
  • Cool. You can always answer your own question, which is beneficial for others looking at this question in the future. – Brandon Taylor Jul 24 '13 at 11:25