97๐
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.
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());
- [Django]-Heroku, postgreSQL, django, comments, tastypie: No operator matches the given name and argument type(s). You might need to add explicit type casts
- [Django]-Database returned an invalid value in QuerySet.dates()
- [Django]-Python/Django: log to console under runserver, log to file under Apache
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>
- [Django]-Celery โ Get task id for current task
- [Django]-Can I have a Django model that has a foreign key reference to itself?
- [Django]-Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?
- [Django]-Django form fails validation on a unique field
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-Error: No module named psycopg2.extensions
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.
- [Django]-Why is Django throwing error "DisallowedHost at /"?
- [Django]-How to write a unit test for a django view?
- [Django]-Django-Forms with json fields
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.
- [Django]-How to get primary keys of objects created using django bulk_create
- [Django]-Django: remove a filter condition from a queryset
- [Django]-Reverse for '*' with arguments '()' and keyword arguments '{}' not found