2

How can I use template variable in dynamic url. I want something like this:

href="{% url '{{ questionnaire.url }}' %}"

The problem is that I get this error:

Reverse for '{{ questionnaire.url }}' not found. '{{ questionnaire.url }}' is not a valid view function or pattern name.

Update:

The problem is here in my javascript:

<script>
let el = `<a href="{% url '${questionnaire.url}' %}"></a>
</script>

I insert this code in my template dynamic, and the Django template cant resolve the url correctly.

2 Answers 2

2

Take a look at url documentation. You can pass a view name and Django will resolve it:

view.py

path('some-url/', app_views.client, name='app-views-client')

index.html

<a href="{% url 'app-views-client' %}" >Go</a>
Sign up to request clarification or add additional context in comments.

1 Comment

i know but I get the view name from this {{ questionnaire.url }}.
0

You can use with template tag

{% with my_url=questionnaire.url %}
   <a href="{% url my_url %}">Go</a>
{% endwith %}

7 Comments

Then the error is here: href="{% url {{ my_url }} %}" . Could not parse the remainder: '{{' from '{{'.
remove {{ }} just keep my_url
I keep getting an error. Reverse for 'my_url' not found. 'my_url' is not a valid view function or pattern name.
just print what y get in my_url
It does not print out anything.
|

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.