kxdan
kxdan

Reputation: 187

Retrieve clicked list item in Django

I'm using a list to populate / generate some html code dynamically as shown below,

        <ul>
        {% if results %}
            {% for result in results %}
                <li><a href="/accounts/evalpage/" ><strong>{{result.0}}</strong>{{result.1}}</a></li>
                {% endfor %}
                {% else %}
                <li><a href="#"><strong>No Evaluations to Perform</strong></a></li>
            {% endif %}
        </ul>

I'm running into an issue with that if one of the list items is clicked, i need to be able to retrieve the information stored in that list item, e.g if item one is clicked i need to retrieve {{result.0}} and {{result.1}} is there a way i can retrieve this information?

List is shown below :

[['User1', 'InProgress'], ['User2'], ['User3'], ['User3'], ['User4']]

For instance, if the row containing User1 and InProgress is clicked by the end user, i want to be able to have the information User1 and InProgress within Django such that operations can be performed with them

Upvotes: 1

Views: 1391

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599758

It should be clear that in order for the backend to know what object you clicked on, you need to pass that value in the URL. So, you'll need to change the definition of your "evalpage" URL to accept a parameter:

url(r'^accounts/evalpage/(?P<user>\w+)/$', views.evalpage, name='evalpage')

and the view signature:

def evalpage(request, user):
    ...

and now you can do:

<a href="{% url "evalpage" user=result.0 %}">...</a>

Upvotes: 2

Ajay Gupta
Ajay Gupta

Reputation: 1285

try this:

 <ul>
            {% if results %}
                {% for result in results %}
                    <li>
                <a href="/accounts/evalpage/" >
                    {% for item in result %}
                    <strong>{{item}}</strong>
                    {% endfor %}
                </a>
            </li>
                    {% endfor %}
                    {% else %}
                    <li><a href="#"><strong>No Evaluations to Perform</strong></a></li>
                {% endif %}
            </ul>

Upvotes: 1

Related Questions