Reputation: 802
I'm having some trouble passing a string parameter to a href
from withing a template.
I have the following url pattern
path('current_observations/<str:mongo_id>',
views.specific_observation,
name='specific_observation')
At current_observations
I have several objects which have the mongo ID as an attribute. The idea is to send this id to the new url.
I tried doing the following without success
<a href="{% url 'VM_OrchestratorApp:specific_observation' resource.id|stringformat:s %}">
{{ resource.TITLE }}
</a>
Which returns the error Failed to lookup for key [s]
Tried doing the following also
<a href="{% url 'VM_OrchestratorApp:specific_observation' resource.id|stringformat:'s' %}">
{{ resource.TITLE }}
</a>
But in this case the argument is not being recognized at all.
NOTE: By doing
<a href="{% url 'VM_OrchestratorApp:specific_observation' 'hello' %}">
{{ resource.TITLE }}
</a>
I get redirected to current_observations/hello
which is the expected behavior.
Any idea on what could be going wrong with the string formatting?
EDIT:
Based on @Ralf answer, I tried doing the following.
<a href="{% url 'VM_OrchestratorApp:specific_observation' resource.id %}">
{{ resource.TITLE }}
</a>
This returns the following error.
Reverse for 'specific_observation' with arguments '('Some_id',)' not found. 1 pattern(s) tried: ['current_observations/(?P<mongo_id>[^/]+)$']
It seems like a second empty argument is being added when calling the specific_observation
url.
Upvotes: 0
Views: 147
Reputation: 16515
Have you tried just using resource.id
without the template filter stringformat
?
The url
template filter docs show an example of letting the filter handle the conversion to string (code example {% url 'app-views-client' client.id %}
, located a few paragraphs into the description of this filter).
The code would look like this:
<a href="{% url 'VM_OrchestratorApp:specific_observation' resource.id %}">
{{ resource.TITLE }}
</a>
Upvotes: 1