【发布时间】:2021-01-08 03:05:08
【问题描述】:
我一直在寻找这个问题,虽然有很多线程,但大多数使用 javascrip、ajax 或其他我不知道的东西......
我有一个名为 movies 的列表,其中包含电影列表及其所有信息。
我正在使用它在表格中显示所有电影。
{% 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 %}
我的模态框的修剪版本如下所示:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalTitle"
<div class="modal-body">
{{movies.0}}
</div>
</div>
最后我的views.py 看起来像这样。
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)
显然,无论我点击哪部电影,我都只会得到第一部电影的描述,因为{{movies.0}}。我想知道如何将正确的索引传递给模态,以便我可以执行类似{{movies.pk}} 之类的操作,其中pk 将是与我按下的电影相对应的索引?
【问题讨论】:
-
向我们展示您的
views.py或您正在谈论的列表。