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.