Reputation: 164
I've been looking around for this problem and while there are many threads, most use javascrip, ajax, or other stuff that I don't know...
I have a list called movies
that contain lists of movies and all their information.
I'm using this to display all movies in a table.
{% for movie in movies %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td>
<a type="button" data-toggle="modal"
data-target="#myModal">
{{movie.0}} <!-- The movie name-->
</a>
</td>
<td>{{movie.3}}</td>. <!-- The movie rating-->
<td><a href="">Add</a></td>
</tr>
{% endfor %}
And a trimmed version of my modal looks like:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalTitle"
<div class="modal-body">
{{movies.0}}
</div>
</div>
Lastly my views.py
looks like this.
def search_movie(request):
info_for_all_movies = []
recommender = MovieRecommender()
form = MovieForm(request.POST)
if request.method == 'POST':
if 'search_movie' in request.POST:
movie = request.POST.get('name')
movies = recommender.recommend_movie(movie) # returns a list containing the name, director, rating, description, etc...
for movie in movies:
movie_info = recommender.get_movie_info(movie)
info_for_all_movies.append(movie_info)
context = {'movies': info_for_all_movies, 'form': form}
return render(request, 'movie/dashboard.html', context)
Obviously, no matter which movie I click i will only get the description of the first movie because of {{movies.0}}
. Im wondering how can I pass the correct index to the modal so I can do maybe something like {{movies.pk}}
where pk
would be the index corresponding to the movie I pressed?
Upvotes: 0
Views: 482
Reputation: 164
Feel kinda dumb as it was pretty simple even though maybe not the most efficient solution. I named my button id="myModal{{movie}}"
and my modal data-target="#myModal{{movie}}">
.
Found the answer at: pass value to bootstrap modal form with django
There is a better solution with AJAX but I dont know where to put that code.
Upvotes: 1