简体   繁体   English

Django for 循环仅在按钮单击时捕获最后一次迭代

[英]Django for loop only catching the last iteration on button click

I am building a Django movie app which makes use of the TMDB API.我正在构建一个使用 TMDB API 的 Django 电影应用程序。 The way I have it set up currently is that I have a function in my main.js which is run when the page is loaded which makes the call to the API based on which media_id is clicked from the search results, which saves to local storage.我目前设置它的方式是我的 main.js 中有一个 function,它在加载页面时运行,它根据从搜索结果中单击的 media_id 调用 API,它保存到本地存储. This works fine in this regard.这在这方面工作得很好。

I implemented a review functionality where logged in users are able to leave reviews on whichever title they wish and it saves to the DB, and then on the user account page I have a list displayed of all the reviews a logged in user has left.我实现了一个评论功能,登录的用户可以留下他们想要的任何标题的评论并将其保存到数据库中,然后在用户帐户页面上,我有一个显示登录用户留下的所有评论的列表。 Up till here everything works as intended but this is where my error lies.到目前为止,一切都按预期工作,但这是我的错误所在。

Next to each review in the users account page I have a button I want to take the user to the page in which they left the review, however I can only seem to get it to link me to the final title in the list of reviews.在用户帐户页面中的每条评论旁边,我都有一个按钮,我想将用户带到他们留下评论的页面,但是我似乎只能让它将我链接到评论列表中的最终标题。

The code I have in my account.html file is as follows:我的 account.html 文件中的代码如下:

<tbody>
    {% for review in reviews %}
        <script type="text/javascript">
            function resetValues() {
                localStorage.setItem('movieId', "{{review.media_id}}");
                localStorage.setItem('title', "{{review.media_title}}");
              }
          </script>
          <tr>
            <td>{{ review.media_title }}</td>
            <td>{{ review.review_title }}</td>
            <td>
              <button type="button" class="btn btn-light" style="float: right;" onclick='resetValues()' href="{% url 'result' %}">View Title</button>
            </td>
          </tr>
    {% endfor %}
</tbody>

And the code in main.js for getting the result from the API call: main.js 中用于从 API 调用中获取结果的代码:

function getTitle() {
    let movieId = localStorage.getItem('movieId');
    axios.get('https://api.themoviedb.org/3/movie/'+movieId+'?api_key={API_KEY}&language=en-US&append_to_response=credits').then((response) => {
        let movie = response.data;
        ...

All reviews display as they should, just the button will always take me to the page of the final review in the list.所有评论都按应有的方式显示,只是按钮将始终将我带到列表中最终评论的页面。

Ideally I would like to fix this issue in here rather than change any of the result views as its currently working from the homepage or from user search, but of course if it cannot be avoided I am happy to change it.理想情况下,我想在这里解决这个问题,而不是更改任何结果视图,因为它当前在主页或用户搜索中工作,但当然,如果无法避免,我很乐意更改它。

I am still relatively new to Django and web development so please forgive me if this seems trivial or messy.我对 Django 和 web 开发还比较陌生,所以如果这看起来微不足道或混乱,请原谅我。 Thank you.谢谢你。

Why use the localStorage to store the movie id?为什么要使用localStorage来存储电影 id? Also you set it in each iteration of the loop overriding all previous values.您还可以在循环的每次迭代中设置它,覆盖所有先前的值。 You can easily set the movie id on your button and get it in the function.您可以轻松地在按钮上设置电影 ID,并在 function 中获取它。

In your HTML :在您的HTML 中

<button type="button" class="btn btn-light" style="float: right;" onclick='getTitle(this)' data-movie-id="{{ review.media_id }}" href="{% url 'result' %}">View Title</button>

Notice getTitle(this) and the extra attribute data-movie-id .注意getTitle(this)和额外的属性data-movie-id

Now in your JavaScript :现在在您的JavaScript中:

function getTitle(elem) {
    let movieId = elem.getAttribute("data-movie-id");
    ...

Indeed you're overriding the definition of the resetValues function at every cycle in the for-loop.实际上,您在 for 循环中的每个周期都覆盖了resetValues function 的定义。

A quick and ugly fix would be to use the id of a review in the definition of the function:一个快速而丑陋的解决方法是在 function 的定义中使用评论的 id:

function resetValues{{review.media_id}}() {

Then for the button:然后对于按钮:

<button type="button" class="btn btn-light" style="float: right;" onclick="resetValues{{review.media_id}}()" href="{% url 'result' %}">
    View Title
</button>

But I suggest following @abdul-aziz-barkat answer !但我建议遵循@abdul-aziz-barkat 的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM