Katie Melosto
Katie Melosto

Reputation: 1177

how to insert a python variable inside a hyperlink in html using Django?

In this html block:

{% block body %}
    <h1>All Pages</h1>

    <ul>
        {% for x in entries %}
            {% comment %} {% wik = 'mask/' + x + '.md' %} {% endcomment %}
            <li><a href = 'mask/'{{x}} </a>{{ x }}</li>
        {% endfor %}
    </ul>

{% endblock %}

I am trying to insert a string in x that will go to end of the hypertext. mask/cat, where "cat" is x [just as an example]. When I do this, the entry does not seem to be added to the hyperlink when I click it.

The other way I thought is to create a python variable wik with the correct page hyperlink, but then I get an issue in Django

Invalid block tag on line 12: 'wik', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

Any help appreciated thanks

Upvotes: 1

Views: 622

Answers (1)

Danielle
Danielle

Reputation: 147

Your problem here is that your variable is outside of the href, this should work:

<li><a href = 'mask/{{x}}' </a>{{ x }}</li>

If it's a variable based url on your app though, I would consider using a url pattern for a cleaner solution

Upvotes: 2

Related Questions