获取已循环的每个项目的名称。

huangapple go评论93阅读模式
英文:

How can i get the name of each item that has been looped?

问题

You want to dynamically change the title of a modal in your Django project based on the item you click on. To do this, you can modify your code as follows:

  1. In your HTML template, update the modal title to use a dynamic value, like this:
<h5 class="modal-title" id="exampleModalLabel">{{ pizza.name|title }}</h5>
  1. In your JavaScript code (if you have any), make sure you're correctly passing the item's name to the modal when you open it. You might need to use JavaScript or a JavaScript framework like jQuery to achieve this.

  2. Ensure that when you click on the "Customize" button for a specific item, you're passing the correct item's name to the modal. You might need to adjust the JavaScript code that triggers the modal to include the item's name as a parameter.

By following these steps, you should be able to display the correct item name in the modal title when clicking on the "Customize" button for each item.

英文:

How do I get the name of each item from the looped Django model? I am doing a little project and I have hit a wall on this,

&lt;div class=&quot;row row-cols-1 row-cols-md-4 g-4 mb-4&quot;&gt;

  {% for pizza in pizzas %}

    

  &lt;div class=&quot;col&quot;&gt;
    &lt;div class=&quot;card h-100&quot;&gt;
      &lt;img src=&quot;...&quot; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;
      &lt;div class=&quot;card-body&quot;&gt;
        &lt;h5 class=&quot;card-title&quot;&gt;**{{pizza.name|title}}**&lt;/h5&gt;
        &lt;p class=&quot;card-text&quot;&gt;This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class=&quot;card-footer d-flex justify-content-between&quot;&gt;
        &lt;a href=&quot;{% url &#39;store:custompizza&#39; pk=pizza.pk %}&quot; class=&quot;btn btn-primary btn-sm&quot; data-bs-toggle=&quot;modal&quot; data-bs-target=&quot;#exampleModal&quot;&gt;Customize&lt;/a&gt;
        
        &lt;!-- Modal --&gt;
        &lt;div class=&quot;modal fade&quot; id=&quot;exampleModal&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
          &lt;div class=&quot;modal-dialog modal-dialog-centered&quot;&gt;
            &lt;div class=&quot;modal-content&quot;&gt;
              &lt;div class=&quot;modal-header&quot;&gt;
                &lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;{{pizza.name|title}}&lt;/h5&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
              &lt;/div&gt;
              &lt;div class=&quot;modal-body&quot;&gt;
                &lt;div class=&quot;form-check&quot;&gt;
                  &lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;defaultCheck1&quot;&gt;
                  &lt;label class=&quot;form-check-label&quot; for=&quot;defaultCheck1&quot;&gt;
                    Default checkbox
                  &lt;/label&gt;
              &lt;div class=&quot;modal-footer&quot;&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-danger&quot; data-bs-dismiss=&quot;modal&quot;&gt;Cancel&lt;/button&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn btn-warning&quot;&gt;Add to cart&lt;/button&gt;
              &lt;/div&gt; 
             &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;

        &lt;small class=&quot;fw-bold&quot;&gt;${{pizza.price}}&lt;/small&gt;

The model:

class pizza(models.Model):
    name = models.CharField(max_length=40, blank=True)
    price = models.DecimalField(decimal_places=2, max_digits=3)

    def get_absolute_url(self):
        return reverse(&quot;store:custompizza&quot;, kwargs={&quot;pk&quot;: self.pk})

The View:

class PizzaList(ListView):
    model = pizza
    context_object_name = &#39;pizzas&#39;

So what i'm trying to do in this little project is for each item that I looped I want the title of the modal to be dynamic and change according to the item that I click on, but it just doesn't change it adamantly continues to display the name of the first item. 获取已循环的每个项目的名称。

So,After everything has been looped and displayed on the page I click on a button, in this case it's the customize button,on any of the item to activate the modal and at the top of the modal I get the title of the first item that's stored in the database not the one specifically want to show. for example:

if I click on the pineapple card's customize button it shows the title of the first item, Plain. And not pineapple like I want it to. I have attached a picture in case I'm not explaining myself accurately
获取已循环的每个项目的名称。

I tried to use get_absolute_url() with a pk to get the title of each item and it did not work, I kept getting the name of the first item for whatever item I click on. Can someone guide me? I'm new to Django and it seems I hit a wall on this.

答案1

得分: 3

The problem is with the way you are using the modal id in your HTML.

In this bit of HTML:

<div class="card-footer d-flex justify-content-between">
    <a href="{% url 'store:custompizza' pk=pizza.pk %}" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#exampleModal">Customize</a>

    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">

You need to make the modal ID unique for each iteration through pizzas. You can do this using the forloop counter or the ID of the pizza object (which will be unique). Something like this:

<div class="card-footer d-flex justify-content-between">
    <a href="{% url 'store:custompizza' pk=pizza.pk %}" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#exampleModal{{pizza.id}}">Customize</a>

    <!-- Modal -->
    <div class="modal fade" id="exampleModal{{pizza.id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
英文:

The problem is with the way you are using the modal id in your HTML.

In this bit of HTML:

&lt;div class=&quot;card-footer d-flex justify-content-between&quot;&gt;
    &lt;a href=&quot;{% url &#39;store:custompizza&#39; pk=pizza.pk %}&quot; class=&quot;btn btn-primary btn-sm&quot; data-bs-toggle=&quot;modal&quot; data-bs-target=&quot;#exampleModal&quot;&gt;Customize&lt;/a&gt;
    
    &lt;!-- Modal --&gt;
    &lt;div class=&quot;modal fade&quot; id=&quot;exampleModal&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;

You need to make the modal ID unique for each iteration through pizzas. You can do this using the forloop counter, or the id of the pizza object (which will be unique). Something like this:

&lt;div class=&quot;card-footer d-flex justify-content-between&quot;&gt;
    &lt;a href=&quot;{% url &#39;store:custompizza&#39; pk=pizza.pk %}&quot; class=&quot;btn btn-primary btn-sm&quot; data-bs-toggle=&quot;modal&quot; data-bs-target=&quot;#exampleModal{{pizza.id}}&quot;&gt;Customize&lt;/a&gt;
    
    &lt;!-- Modal --&gt;
    &lt;div class=&quot;modal fade&quot; id=&quot;exampleModal{{pizza.id}}&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月21日 17:32:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定