简体   繁体   中英

Get javascript variable's value in Django url template tag

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?

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.

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());

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>
var name = "{% url 'your_view_name' pk=0 %}".replace('0', name_variable);

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.

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.

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