How to use Django for loop twice on the same data2019 Community Moderator ElectionHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?Accessing the index in 'for' loops?How do I sort a dictionary by value?Does Django scale?How do I list all files of a directory?Iterating over dictionaries using 'for' loopsCannot display HTML string

Why would /etc/passwd be used every time someone executes `ls -l` command?

How to distinguish easily different soldier of ww2?

How can I have x-axis ticks that show ticks scaled in powers of ten?

Create chunks from an array

What is better: yes / no radio, or simple checkbox?

Why does a car's steering wheel get lighter with increasing speed

Paper published similar to PhD thesis

The (Easy) Road to Code

Interpretation of linear regression interaction term plot

What is the purpose of a disclaimer like "this is not legal advice"?

Is it a Cyclops number? "Nobody" knows!

How to install "rounded" brake pads

Limpar string com Regex

A running toilet that stops itself

What is the orbit and expected lifetime of Crew Dragon trunk?

Why restrict private health insurance?

Generating a list with duplicate entries

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?

If nine coins are tossed, what is the probability that the number of heads is even?

Professor forcing me to attend a conference, I can't afford even with 50% funding

How spaceships determine each other's mass in space?

How to make sure I'm assertive enough in contact with subordinates?

Was this cameo in Captain Marvel computer generated?

Book where society has been split into 2 with a wall down the middle where one side embraced high tech whereas other side were totally against tech



How to use Django for loop twice on the same data



2019 Community Moderator ElectionHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?Accessing the index in 'for' loops?How do I sort a dictionary by value?Does Django scale?How do I list all files of a directory?Iterating over dictionaries using 'for' loopsCannot display HTML string










1















I'm attempting to use a for loop to iterate over the same data twice in the same Django template. However the second for loop only returns the first object in the database.



Basically I have an image of each job in the database on the homepage and when clicked on it should display a more detailed description of that job. All the jobs from the database display fine on the homepage but they all display the description of the first job when clicked on.



I think it's something to do with only being able to use an iterator once but I can't figure out what the solution is.



model.py



from django.db import models

class Job(models.Model):
title = models.CharField(max_length=50, default="Example Work")
image = models.ImageField(upload_to='images/')
summary = models.TextField()


views.py



from django.shortcuts import render
from .models import Job

def get_index(request):
jobs = Job.objects
return render(request, 'index.html', 'jobs': jobs)


index.html






% for job in jobs.all %
<div class="col-md-6 col-lg-4">
<a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
<div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
<div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
<i class="fas fa-search-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src=" job.image.url " alt="">
</a>
<h3 class="text-center text-secondary"> job.title </h3>
</div>
% endfor %
<!-- Portfolio Modal -->
% for job in jobs.all %
<div class="portfolio-modal mfp-hide" id="portfolio-modal">
<div class="portfolio-modal-dialog bg-white">
<a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
<i class="fa fa-3x fa-times"></i>
</a>
<div class="container text-center">
<div class="row">
<div class="col-lg-8 mx-auto">
<h2 class="text-secondary text-uppercase mb-0"> job.title </h2>
<hr class="star-dark mb-5">
<img class="img-fluid mb-5" src=" job.image.url " alt="Image of website">
<p class="mb-5"> job.summary </p>
<a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
<i class="fa fa-close"></i>Close Project</a>
</div>
</div>
</div>
</div>
</div>
% endfor %





Just for context, I've ripped out a lot of the html code from index.html because that all works fine and it probably isn't relevant. I've tried it in within one loop which doesn't work either. The description opens up within a new window on the same page. I've tried anything I can think of but not getting anywhere with it. Still getting to grips with Django and Python. Any help appreciated.










share|improve this question







New contributor




web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    1















    I'm attempting to use a for loop to iterate over the same data twice in the same Django template. However the second for loop only returns the first object in the database.



    Basically I have an image of each job in the database on the homepage and when clicked on it should display a more detailed description of that job. All the jobs from the database display fine on the homepage but they all display the description of the first job when clicked on.



    I think it's something to do with only being able to use an iterator once but I can't figure out what the solution is.



    model.py



    from django.db import models

    class Job(models.Model):
    title = models.CharField(max_length=50, default="Example Work")
    image = models.ImageField(upload_to='images/')
    summary = models.TextField()


    views.py



    from django.shortcuts import render
    from .models import Job

    def get_index(request):
    jobs = Job.objects
    return render(request, 'index.html', 'jobs': jobs)


    index.html






    % for job in jobs.all %
    <div class="col-md-6 col-lg-4">
    <a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
    <div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
    <div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
    <i class="fas fa-search-plus fa-3x"></i>
    </div>
    </div>
    <img class="img-fluid" src=" job.image.url " alt="">
    </a>
    <h3 class="text-center text-secondary"> job.title </h3>
    </div>
    % endfor %
    <!-- Portfolio Modal -->
    % for job in jobs.all %
    <div class="portfolio-modal mfp-hide" id="portfolio-modal">
    <div class="portfolio-modal-dialog bg-white">
    <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
    <i class="fa fa-3x fa-times"></i>
    </a>
    <div class="container text-center">
    <div class="row">
    <div class="col-lg-8 mx-auto">
    <h2 class="text-secondary text-uppercase mb-0"> job.title </h2>
    <hr class="star-dark mb-5">
    <img class="img-fluid mb-5" src=" job.image.url " alt="Image of website">
    <p class="mb-5"> job.summary </p>
    <a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
    <i class="fa fa-close"></i>Close Project</a>
    </div>
    </div>
    </div>
    </div>
    </div>
    % endfor %





    Just for context, I've ripped out a lot of the html code from index.html because that all works fine and it probably isn't relevant. I've tried it in within one loop which doesn't work either. The description opens up within a new window on the same page. I've tried anything I can think of but not getting anywhere with it. Still getting to grips with Django and Python. Any help appreciated.










    share|improve this question







    New contributor




    web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      1












      1








      1








      I'm attempting to use a for loop to iterate over the same data twice in the same Django template. However the second for loop only returns the first object in the database.



      Basically I have an image of each job in the database on the homepage and when clicked on it should display a more detailed description of that job. All the jobs from the database display fine on the homepage but they all display the description of the first job when clicked on.



      I think it's something to do with only being able to use an iterator once but I can't figure out what the solution is.



      model.py



      from django.db import models

      class Job(models.Model):
      title = models.CharField(max_length=50, default="Example Work")
      image = models.ImageField(upload_to='images/')
      summary = models.TextField()


      views.py



      from django.shortcuts import render
      from .models import Job

      def get_index(request):
      jobs = Job.objects
      return render(request, 'index.html', 'jobs': jobs)


      index.html






      % for job in jobs.all %
      <div class="col-md-6 col-lg-4">
      <a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
      <div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
      <div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
      <i class="fas fa-search-plus fa-3x"></i>
      </div>
      </div>
      <img class="img-fluid" src=" job.image.url " alt="">
      </a>
      <h3 class="text-center text-secondary"> job.title </h3>
      </div>
      % endfor %
      <!-- Portfolio Modal -->
      % for job in jobs.all %
      <div class="portfolio-modal mfp-hide" id="portfolio-modal">
      <div class="portfolio-modal-dialog bg-white">
      <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
      <i class="fa fa-3x fa-times"></i>
      </a>
      <div class="container text-center">
      <div class="row">
      <div class="col-lg-8 mx-auto">
      <h2 class="text-secondary text-uppercase mb-0"> job.title </h2>
      <hr class="star-dark mb-5">
      <img class="img-fluid mb-5" src=" job.image.url " alt="Image of website">
      <p class="mb-5"> job.summary </p>
      <a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
      <i class="fa fa-close"></i>Close Project</a>
      </div>
      </div>
      </div>
      </div>
      </div>
      % endfor %





      Just for context, I've ripped out a lot of the html code from index.html because that all works fine and it probably isn't relevant. I've tried it in within one loop which doesn't work either. The description opens up within a new window on the same page. I've tried anything I can think of but not getting anywhere with it. Still getting to grips with Django and Python. Any help appreciated.










      share|improve this question







      New contributor




      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm attempting to use a for loop to iterate over the same data twice in the same Django template. However the second for loop only returns the first object in the database.



      Basically I have an image of each job in the database on the homepage and when clicked on it should display a more detailed description of that job. All the jobs from the database display fine on the homepage but they all display the description of the first job when clicked on.



      I think it's something to do with only being able to use an iterator once but I can't figure out what the solution is.



      model.py



      from django.db import models

      class Job(models.Model):
      title = models.CharField(max_length=50, default="Example Work")
      image = models.ImageField(upload_to='images/')
      summary = models.TextField()


      views.py



      from django.shortcuts import render
      from .models import Job

      def get_index(request):
      jobs = Job.objects
      return render(request, 'index.html', 'jobs': jobs)


      index.html






      % for job in jobs.all %
      <div class="col-md-6 col-lg-4">
      <a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
      <div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
      <div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
      <i class="fas fa-search-plus fa-3x"></i>
      </div>
      </div>
      <img class="img-fluid" src=" job.image.url " alt="">
      </a>
      <h3 class="text-center text-secondary"> job.title </h3>
      </div>
      % endfor %
      <!-- Portfolio Modal -->
      % for job in jobs.all %
      <div class="portfolio-modal mfp-hide" id="portfolio-modal">
      <div class="portfolio-modal-dialog bg-white">
      <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
      <i class="fa fa-3x fa-times"></i>
      </a>
      <div class="container text-center">
      <div class="row">
      <div class="col-lg-8 mx-auto">
      <h2 class="text-secondary text-uppercase mb-0"> job.title </h2>
      <hr class="star-dark mb-5">
      <img class="img-fluid mb-5" src=" job.image.url " alt="Image of website">
      <p class="mb-5"> job.summary </p>
      <a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
      <i class="fa fa-close"></i>Close Project</a>
      </div>
      </div>
      </div>
      </div>
      </div>
      % endfor %





      Just for context, I've ripped out a lot of the html code from index.html because that all works fine and it probably isn't relevant. I've tried it in within one loop which doesn't work either. The description opens up within a new window on the same page. I've tried anything I can think of but not getting anywhere with it. Still getting to grips with Django and Python. Any help appreciated.






      % for job in jobs.all %
      <div class="col-md-6 col-lg-4">
      <a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
      <div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
      <div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
      <i class="fas fa-search-plus fa-3x"></i>
      </div>
      </div>
      <img class="img-fluid" src=" job.image.url " alt="">
      </a>
      <h3 class="text-center text-secondary"> job.title </h3>
      </div>
      % endfor %
      <!-- Portfolio Modal -->
      % for job in jobs.all %
      <div class="portfolio-modal mfp-hide" id="portfolio-modal">
      <div class="portfolio-modal-dialog bg-white">
      <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
      <i class="fa fa-3x fa-times"></i>
      </a>
      <div class="container text-center">
      <div class="row">
      <div class="col-lg-8 mx-auto">
      <h2 class="text-secondary text-uppercase mb-0"> job.title </h2>
      <hr class="star-dark mb-5">
      <img class="img-fluid mb-5" src=" job.image.url " alt="Image of website">
      <p class="mb-5"> job.summary </p>
      <a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
      <i class="fa fa-close"></i>Close Project</a>
      </div>
      </div>
      </div>
      </div>
      </div>
      % endfor %





      % for job in jobs.all %
      <div class="col-md-6 col-lg-4">
      <a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
      <div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
      <div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
      <i class="fas fa-search-plus fa-3x"></i>
      </div>
      </div>
      <img class="img-fluid" src=" job.image.url " alt="">
      </a>
      <h3 class="text-center text-secondary"> job.title </h3>
      </div>
      % endfor %
      <!-- Portfolio Modal -->
      % for job in jobs.all %
      <div class="portfolio-modal mfp-hide" id="portfolio-modal">
      <div class="portfolio-modal-dialog bg-white">
      <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
      <i class="fa fa-3x fa-times"></i>
      </a>
      <div class="container text-center">
      <div class="row">
      <div class="col-lg-8 mx-auto">
      <h2 class="text-secondary text-uppercase mb-0"> job.title </h2>
      <hr class="star-dark mb-5">
      <img class="img-fluid mb-5" src=" job.image.url " alt="Image of website">
      <p class="mb-5"> job.summary </p>
      <a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
      <i class="fa fa-close"></i>Close Project</a>
      </div>
      </div>
      </div>
      </div>
      </div>
      % endfor %






      python django






      share|improve this question







      New contributor




      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      web_crawlerweb_crawler

      62




      62




      New contributor




      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      web_crawler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1














          This isn't anything to do with Django outputting the same data for each iteration. If you check the generated HTML via View Source in your browser, you'll definitely see the correct data.



          The problem is with your HTML itself. You cannot have multiple elements with the same HTML id attribute; but you are using id="portfolio-modal" for each one. When your JS tries to pop up the modal, it will only ever find the first instance of this ID.



          You need to use a different ID in each iteration - perhaps by appending a unique element, eg the Job pk - and/or use a class to trigger your modal.






          share|improve this answer























          • Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

            – web_crawler
            yesterday










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          web_crawler is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023532%2fhow-to-use-django-for-loop-twice-on-the-same-data%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This isn't anything to do with Django outputting the same data for each iteration. If you check the generated HTML via View Source in your browser, you'll definitely see the correct data.



          The problem is with your HTML itself. You cannot have multiple elements with the same HTML id attribute; but you are using id="portfolio-modal" for each one. When your JS tries to pop up the modal, it will only ever find the first instance of this ID.



          You need to use a different ID in each iteration - perhaps by appending a unique element, eg the Job pk - and/or use a class to trigger your modal.






          share|improve this answer























          • Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

            – web_crawler
            yesterday















          1














          This isn't anything to do with Django outputting the same data for each iteration. If you check the generated HTML via View Source in your browser, you'll definitely see the correct data.



          The problem is with your HTML itself. You cannot have multiple elements with the same HTML id attribute; but you are using id="portfolio-modal" for each one. When your JS tries to pop up the modal, it will only ever find the first instance of this ID.



          You need to use a different ID in each iteration - perhaps by appending a unique element, eg the Job pk - and/or use a class to trigger your modal.






          share|improve this answer























          • Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

            – web_crawler
            yesterday













          1












          1








          1







          This isn't anything to do with Django outputting the same data for each iteration. If you check the generated HTML via View Source in your browser, you'll definitely see the correct data.



          The problem is with your HTML itself. You cannot have multiple elements with the same HTML id attribute; but you are using id="portfolio-modal" for each one. When your JS tries to pop up the modal, it will only ever find the first instance of this ID.



          You need to use a different ID in each iteration - perhaps by appending a unique element, eg the Job pk - and/or use a class to trigger your modal.






          share|improve this answer













          This isn't anything to do with Django outputting the same data for each iteration. If you check the generated HTML via View Source in your browser, you'll definitely see the correct data.



          The problem is with your HTML itself. You cannot have multiple elements with the same HTML id attribute; but you are using id="portfolio-modal" for each one. When your JS tries to pop up the modal, it will only ever find the first instance of this ID.



          You need to use a different ID in each iteration - perhaps by appending a unique element, eg the Job pk - and/or use a class to trigger your modal.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Daniel RosemanDaniel Roseman

          454k41587644




          454k41587644












          • Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

            – web_crawler
            yesterday

















          • Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

            – web_crawler
            yesterday
















          Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

          – web_crawler
          yesterday





          Ah ok thanks, that makes sense. Originally I had modals with different ID's for each one but I had condensed it into the loop and forgot about that. Thanks for the help.

          – web_crawler
          yesterday












          web_crawler is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          web_crawler is a new contributor. Be nice, and check out our Code of Conduct.












          web_crawler is a new contributor. Be nice, and check out our Code of Conduct.











          web_crawler is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023532%2fhow-to-use-django-for-loop-twice-on-the-same-data%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко