How to refer to set objects in a django view from a template?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have data in a database in the form like this:



collection_name|manufacturer|product_type|description|image_url
----------------------------------------------------------------
Testing |FakeCo |Bed |pretty nice|/img/1.jpg
Testing |FakeCo |Desk |pretty bad |/img/2.jpg
Testing |FakeCo |Nightstand |pretty ok |/img/1.jpg
Testing |FakeCo |Draws |pretty nice|/img/3.jpg


Initially, I was using a for loop to display fields from each result, which ends up with something like this:this:.



For the example data set above, what I am trying to do is display only the first result from certain fields, knowing they are identical for all rows returned, and then for remaining fields only display them when they are distinct.



I tried using sets in my django view, as another answer suggested this would eliminate duplicates and solve my issue.



My django view:



def collection_detail(request, name=None):
template = loader.get_template('/webapps/my_webapp/furniture_site/main_page/templates/main_page/product-detail.html')
products = product.objects.filter(collection_name=name)
collection_name =
manufacturer =
description =
image_url =
for product in products:
collection_name.append(product.collection_name)
manufacturer.append(product.manufacturer)
description.append(product.description)
image_url.append(product.image_url)
collection_name = set(collection_name)
manufacturer = set(manufacturer)
description = set(description)
image_url = set(image_url)
context={'products': products}
return HttpResponse(template.render(context))


My issue is, that I am unable to refer to these set items in my template.



For example, in my template using:



                {% for instance in products %}
{{ instance.collection_name }} Collection <br />
{% endfor %}


returns nothing, as does



                {% for instance in products %}
{{ collection_name }} Collection <br />
{% endfor %}


What is the correct way to refer to items returned via the view in the template?



Ultimately, I am trying to get a result like the following (note descrption and collection name only used once, and duplicate image urls not returned).



enter image description here










share|improve this question





























    1















    I have data in a database in the form like this:



    collection_name|manufacturer|product_type|description|image_url
    ----------------------------------------------------------------
    Testing |FakeCo |Bed |pretty nice|/img/1.jpg
    Testing |FakeCo |Desk |pretty bad |/img/2.jpg
    Testing |FakeCo |Nightstand |pretty ok |/img/1.jpg
    Testing |FakeCo |Draws |pretty nice|/img/3.jpg


    Initially, I was using a for loop to display fields from each result, which ends up with something like this:this:.



    For the example data set above, what I am trying to do is display only the first result from certain fields, knowing they are identical for all rows returned, and then for remaining fields only display them when they are distinct.



    I tried using sets in my django view, as another answer suggested this would eliminate duplicates and solve my issue.



    My django view:



    def collection_detail(request, name=None):
    template = loader.get_template('/webapps/my_webapp/furniture_site/main_page/templates/main_page/product-detail.html')
    products = product.objects.filter(collection_name=name)
    collection_name =
    manufacturer =
    description =
    image_url =
    for product in products:
    collection_name.append(product.collection_name)
    manufacturer.append(product.manufacturer)
    description.append(product.description)
    image_url.append(product.image_url)
    collection_name = set(collection_name)
    manufacturer = set(manufacturer)
    description = set(description)
    image_url = set(image_url)
    context={'products': products}
    return HttpResponse(template.render(context))


    My issue is, that I am unable to refer to these set items in my template.



    For example, in my template using:



                    {% for instance in products %}
    {{ instance.collection_name }} Collection <br />
    {% endfor %}


    returns nothing, as does



                    {% for instance in products %}
    {{ collection_name }} Collection <br />
    {% endfor %}


    What is the correct way to refer to items returned via the view in the template?



    Ultimately, I am trying to get a result like the following (note descrption and collection name only used once, and duplicate image urls not returned).



    enter image description here










    share|improve this question

























      1












      1








      1








      I have data in a database in the form like this:



      collection_name|manufacturer|product_type|description|image_url
      ----------------------------------------------------------------
      Testing |FakeCo |Bed |pretty nice|/img/1.jpg
      Testing |FakeCo |Desk |pretty bad |/img/2.jpg
      Testing |FakeCo |Nightstand |pretty ok |/img/1.jpg
      Testing |FakeCo |Draws |pretty nice|/img/3.jpg


      Initially, I was using a for loop to display fields from each result, which ends up with something like this:this:.



      For the example data set above, what I am trying to do is display only the first result from certain fields, knowing they are identical for all rows returned, and then for remaining fields only display them when they are distinct.



      I tried using sets in my django view, as another answer suggested this would eliminate duplicates and solve my issue.



      My django view:



      def collection_detail(request, name=None):
      template = loader.get_template('/webapps/my_webapp/furniture_site/main_page/templates/main_page/product-detail.html')
      products = product.objects.filter(collection_name=name)
      collection_name =
      manufacturer =
      description =
      image_url =
      for product in products:
      collection_name.append(product.collection_name)
      manufacturer.append(product.manufacturer)
      description.append(product.description)
      image_url.append(product.image_url)
      collection_name = set(collection_name)
      manufacturer = set(manufacturer)
      description = set(description)
      image_url = set(image_url)
      context={'products': products}
      return HttpResponse(template.render(context))


      My issue is, that I am unable to refer to these set items in my template.



      For example, in my template using:



                      {% for instance in products %}
      {{ instance.collection_name }} Collection <br />
      {% endfor %}


      returns nothing, as does



                      {% for instance in products %}
      {{ collection_name }} Collection <br />
      {% endfor %}


      What is the correct way to refer to items returned via the view in the template?



      Ultimately, I am trying to get a result like the following (note descrption and collection name only used once, and duplicate image urls not returned).



      enter image description here










      share|improve this question














      I have data in a database in the form like this:



      collection_name|manufacturer|product_type|description|image_url
      ----------------------------------------------------------------
      Testing |FakeCo |Bed |pretty nice|/img/1.jpg
      Testing |FakeCo |Desk |pretty bad |/img/2.jpg
      Testing |FakeCo |Nightstand |pretty ok |/img/1.jpg
      Testing |FakeCo |Draws |pretty nice|/img/3.jpg


      Initially, I was using a for loop to display fields from each result, which ends up with something like this:this:.



      For the example data set above, what I am trying to do is display only the first result from certain fields, knowing they are identical for all rows returned, and then for remaining fields only display them when they are distinct.



      I tried using sets in my django view, as another answer suggested this would eliminate duplicates and solve my issue.



      My django view:



      def collection_detail(request, name=None):
      template = loader.get_template('/webapps/my_webapp/furniture_site/main_page/templates/main_page/product-detail.html')
      products = product.objects.filter(collection_name=name)
      collection_name =
      manufacturer =
      description =
      image_url =
      for product in products:
      collection_name.append(product.collection_name)
      manufacturer.append(product.manufacturer)
      description.append(product.description)
      image_url.append(product.image_url)
      collection_name = set(collection_name)
      manufacturer = set(manufacturer)
      description = set(description)
      image_url = set(image_url)
      context={'products': products}
      return HttpResponse(template.render(context))


      My issue is, that I am unable to refer to these set items in my template.



      For example, in my template using:



                      {% for instance in products %}
      {{ instance.collection_name }} Collection <br />
      {% endfor %}


      returns nothing, as does



                      {% for instance in products %}
      {{ collection_name }} Collection <br />
      {% endfor %}


      What is the correct way to refer to items returned via the view in the template?



      Ultimately, I am trying to get a result like the following (note descrption and collection name only used once, and duplicate image urls not returned).



      enter image description here







      django django-templates django-views






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 5:51









      Jake RankinJake Rankin

      18510




      18510
























          2 Answers
          2






          active

          oldest

          votes


















          2














          First of all, you're not passing the right data towards your template.



          You need to pass on collection_name, manufacturer, description and image_url in your context.



          context = {
          'products': products,
          'collection_name': collection_name,
          'manufacturer': manufacturer,
          'description': description,
          'image_url': image_url
          }


          Now you can access these in your template like:



          {% for instance in collection_name %}
          {{ instance }} Collection <br />
          {% endfor %}


          Same for the others.






          share|improve this answer
























          • Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

            – Jake Rankin
            Jan 3 at 6:24



















          1














          It should render only one object in the loop. Still you can use first in your interpolation.



          Like this:



          {{ instance.collection_name|first }}




          EDIT



          You need to pass collection_name as you have initialised it as an empty list therefore it is a variable which you can use only when you pass it in context.



           context={'products': products, 'collection_name': collection_name}





          share|improve this answer


























          • When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

            – Jake Rankin
            Jan 3 at 6:20












          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016986%2fhow-to-refer-to-set-objects-in-a-django-view-from-a-template%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          First of all, you're not passing the right data towards your template.



          You need to pass on collection_name, manufacturer, description and image_url in your context.



          context = {
          'products': products,
          'collection_name': collection_name,
          'manufacturer': manufacturer,
          'description': description,
          'image_url': image_url
          }


          Now you can access these in your template like:



          {% for instance in collection_name %}
          {{ instance }} Collection <br />
          {% endfor %}


          Same for the others.






          share|improve this answer
























          • Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

            – Jake Rankin
            Jan 3 at 6:24
















          2














          First of all, you're not passing the right data towards your template.



          You need to pass on collection_name, manufacturer, description and image_url in your context.



          context = {
          'products': products,
          'collection_name': collection_name,
          'manufacturer': manufacturer,
          'description': description,
          'image_url': image_url
          }


          Now you can access these in your template like:



          {% for instance in collection_name %}
          {{ instance }} Collection <br />
          {% endfor %}


          Same for the others.






          share|improve this answer
























          • Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

            – Jake Rankin
            Jan 3 at 6:24














          2












          2








          2







          First of all, you're not passing the right data towards your template.



          You need to pass on collection_name, manufacturer, description and image_url in your context.



          context = {
          'products': products,
          'collection_name': collection_name,
          'manufacturer': manufacturer,
          'description': description,
          'image_url': image_url
          }


          Now you can access these in your template like:



          {% for instance in collection_name %}
          {{ instance }} Collection <br />
          {% endfor %}


          Same for the others.






          share|improve this answer













          First of all, you're not passing the right data towards your template.



          You need to pass on collection_name, manufacturer, description and image_url in your context.



          context = {
          'products': products,
          'collection_name': collection_name,
          'manufacturer': manufacturer,
          'description': description,
          'image_url': image_url
          }


          Now you can access these in your template like:



          {% for instance in collection_name %}
          {{ instance }} Collection <br />
          {% endfor %}


          Same for the others.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 6:21









          J. GhyllebertJ. Ghyllebert

          1,58412333




          1,58412333













          • Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

            – Jake Rankin
            Jan 3 at 6:24



















          • Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

            – Jake Rankin
            Jan 3 at 6:24

















          Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

          – Jake Rankin
          Jan 3 at 6:24





          Ahhh, it seems so simple once someone explains it, but I would not have known h ow to figure this out for myself. Thank you! Testing now.

          – Jake Rankin
          Jan 3 at 6:24













          1














          It should render only one object in the loop. Still you can use first in your interpolation.



          Like this:



          {{ instance.collection_name|first }}




          EDIT



          You need to pass collection_name as you have initialised it as an empty list therefore it is a variable which you can use only when you pass it in context.



           context={'products': products, 'collection_name': collection_name}





          share|improve this answer


























          • When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

            – Jake Rankin
            Jan 3 at 6:20
















          1














          It should render only one object in the loop. Still you can use first in your interpolation.



          Like this:



          {{ instance.collection_name|first }}




          EDIT



          You need to pass collection_name as you have initialised it as an empty list therefore it is a variable which you can use only when you pass it in context.



           context={'products': products, 'collection_name': collection_name}





          share|improve this answer


























          • When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

            – Jake Rankin
            Jan 3 at 6:20














          1












          1








          1







          It should render only one object in the loop. Still you can use first in your interpolation.



          Like this:



          {{ instance.collection_name|first }}




          EDIT



          You need to pass collection_name as you have initialised it as an empty list therefore it is a variable which you can use only when you pass it in context.



           context={'products': products, 'collection_name': collection_name}





          share|improve this answer















          It should render only one object in the loop. Still you can use first in your interpolation.



          Like this:



          {{ instance.collection_name|first }}




          EDIT



          You need to pass collection_name as you have initialised it as an empty list therefore it is a variable which you can use only when you pass it in context.



           context={'products': products, 'collection_name': collection_name}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 6:26

























          answered Jan 3 at 6:05









          ans2humanans2human

          1,266616




          1,266616













          • When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

            – Jake Rankin
            Jan 3 at 6:20



















          • When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

            – Jake Rankin
            Jan 3 at 6:20

















          When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

          – Jake Rankin
          Jan 3 at 6:20





          When I tried that before using sets in my view, |first gave the first characters of each row+field, not the first result. Also, I need some fields to be multiple within the loop, such as image urls

          – Jake Rankin
          Jan 3 at 6:20


















          draft saved

          draft discarded




















































          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%2f54016986%2fhow-to-refer-to-set-objects-in-a-django-view-from-a-template%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

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules