Thursday, January 3, 2019

Re: Loop over Django objects and Bootstrap cards

how to fetch image from database to template 

On Thu, 3 Jan 2019 at 19:28, valentin jungbluth <valentin.arue@gmail.com> wrote:
Hello guys,

I would like to use Bootstrap cards in order to create one card by object and add some sub_objects in each one.

For example :

I have an object Publication which could contain one or many sub_objects Document.

Publication object has some attributes : category, title, picture, description ...
Document object has some attributes like title, format, language, ...

I would like to get something like this :



For a same category, I create a card by publication and I list all documents for each publication.

This is what I get with my code :



As you can see, I should have document n°1 and document°2 in the same card and not two different cards.

This is my code :

   
{% for category in research_categories|dictsort:'name' %}
     
<div class="row">
       
<fieldset>
         
<legend id="category_{{ category.id }}"><span class="name">{{ category }}</span></legend>
       
</fieldset>
      </
div>
   
       
<div class="row">
         
<div class="col-sm-4">
           
{% for element in test_research %}
             
{% if element.publication.category|stringformat:"s"  == category|stringformat:"s" %}
               
{% ifchanged %}
                 
<div class="card" style="width:250px">
                   
<img class="card-img-top" src="{{ element.publication.cover.url }}" alt="Card image">
                   
<div class="card-body">
                     
<h4 class="card-title">{{ element.publication }}</h4>
   
                     
<table class="table table-condensed">
                       
<tbody>
                       
<tr>
                         
<td> {{ element.title }}</td>
                       
</tr>
                        </
tbody>
                     
</table>
                    </
div>
                 
</div>
                {% endifchanged %}
              {% endif %}
            {% endfor %}
          </
div>
       
</div>
   
{% endfor %}


And my view according to this part is :

   
 # By default, display documents
    test_research
= Document.objects.all().order_by('publication__title', 'title', 'language', 'format')


    research_categories
= defaultdict(list)
   
for element in test_research:
        research_categories
[element.publication.category].append(element)


    research_publications
= defaultdict(list)
   
for element in test_research:
        research_publications
[element.publication].append(element)


    kwargs
['test_research'] = test_research
    kwargs
['research_categories'] = research_categories
    kwargs
['research_publications'] = research_publications


How I can do that ?
Thank you !

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1658ec0f-9769-4c73-a1dd-d68d773bd88e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANwgZcZqRii39EE0U6i%3DC20xJKNChY2ddoBjaOu%3D57U%3DhfJg8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment