0

I am using ajax to generate a table, each row of this table represents a trip with a trip score and multiple cities. I want to add a "AddtoFavourite" button to each trip, once the user clicks on this button a django view will be called which will add the trip to the userss "Favs". Now my question is How to generate this URL for each row where I can pass city, tripname and trip score in the request

Here is the code which generates that table.

Please note datas is the dict which contains list of trips, with each trip having a score and multiple cities in it.

searchresults.html

{% for data in datas %}
<tr>
    <td>{{ data.score}}</td>
    {% for element in data.place.elements %}
    <td>
        <img alt="" src="http://blankket-mk8te7kbzv.elasticbeanstalk.com/img/mapPinSmall.png">{{element.placeName}}
    </td>
    {% endfor %}
    <td>
        <input type="button" onlick=" " id="AddMyTrip" value="Add" class="btn btn-primary"/>
    </td>
</tr>
{% endfor %}
caesarsol
  • 2,010
  • 1
  • 20
  • 21
Mayank
  • 2,333
  • 3
  • 17
  • 23

1 Answers1

0

I don't know your models schema, but i'd say you only need to make one URL to which you will pass slugs (or IDs) for the model to be added to user's favourites.

  • use a GET request if you want to make it simple: <a href="{% url 'vote' %}?trip={{ trip.slug }}">Add as Favourite!</a>

  • use a POST request using AJAX and an onclick action, if you are fancy ;) look here for more info

the target view named vote will read the request.user and the request.REQUEST and make the correct connection in the models (a ManyToMany i guess).

Community
  • 1
  • 1
caesarsol
  • 2,010
  • 1
  • 20
  • 21