0

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.

0

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer!. Its one of the things that I tried but forgot to mention. In this case the url does not match. It returns. 1 pattern(s) tried: ['current_observations/(?P<mongo_id>[^/]+)$'] Tried changing around the pattern but it continues to fail. Ill check the linked docs!
@Manuel you mean this does NOT work for you? Could you describe what happens? (maybe even add that info to your question)
@Manuel I read your edit of the question: what happens if you try my answer together with <str:mongo_id> in the URL conf instead of (?P<mongo_id>[^/]+)$ ?
Sorry for the late answer!, timezones and such. The url pattern (<str:mongo_id>) stayed the same when trying out your suggestion. It seems weird that django is trying out other pattern than the one specified. Also tried out the url conf specified in the error but it did not work. Ill investigate some more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.