简体   繁体   English

如何在 django 视图中显示带有 zip 的内容?

[英]How to show content with zip in view with django?

I hava a django applicaiton.我有一个 Django 应用程序。 And I try to show the content values from the backend in the template.我尝试在模板中显示来自后端的内容值。 There is a method: show_extracted_data_from_file where I combine three methods:有一种方法: show_extracted_data_from_file我结合了三种方法:


class FilterText:

def total_cost_fruit(self):
        return [3588.20, 5018.75, 3488.16, 444]
    
    def total_cost_fruit2(self):
        return [3588.20, 5018.75, 3488.99]
    
    def total_cost_fruit3(self):
        return [3588.20, 5018.75, 44.99]


    def show_extracted_data_from_file(self):

   regexes = [
            self.total_cost_fruit(),
            self.total_cost_fruit2(),
            self.total_cost_fruit3,
        ]
        matches = [(regex) for regex in regexes]

        return zip(matches)

and I have the view:我的观点是:


def test(request):


    content_pdf = ""   
    filter_text = FilterText()     
    content_pdf = filter_text.show_extracted_data_from_file()   
    context = {"content_pdf": content_pdf}
    return render(request, "main/test.html", context)

and html:和 html:

<div class="wishlist">
   <table>
      <tr>
         <th>Method 1</th>
         <th>Method 2</th>
         <th>Method 3</th>
      </tr>
      {% for value in content_pdf %}
      <tr>
         <td>{{value.0}}</td>
         <td>{{value.1}}</td>
         <td>{{value.2}}</td>
      </tr>
      {% endfor %}
   </table>
</div>

But it looks now:但是现在看起来:

Method 1    Method 2    Method 3
[3588.2, 5018.75, 3488.16, 444]         
[3588.2, 5018.75, 3488.99]      
[3588.2, 5018.75, 44.99]

But I want it of course under each other:但我当然希望它在彼此之下:

Method 1    Method 2    Method 3
3588.2           3588.2            3588.2
5018.75,         44.99              3488.99 
5018.75,       
5018.75 
3488.16
444

You can work with itertools.zip_longest(…) [Python-doc] to put None values when one of the methods end, so:您可以使用itertools.zip_longest(…) [Python-doc]在其中一种方法结束时放置None值,因此:

from itertools import zip_longest


class FilterText:
    def total_cost_fruit(self):
        return [3588.20, 5018.75, 3488.16, 444]

    def total_cost_fruit2(self):
        return [3588.20, 5018.75, 3488.99]

    def total_cost_fruit3(self):
        return [3588.20, 5018.75, 44.99]

    def show_extracted_data_from_file(self):
        regexes = [
            self.total_cost_fruit(),
            self.total_cost_fruit2(),
            self.total_cost_fruit3(),
        ]
        return zip_longest(*regexes)

You can also render this more conveniently with:您还可以更方便地渲染它:

<div class="wishlist">
   <table>
      <tr>
         <th>Method 1</th>
         <th>Method 2</th>
         <th>Method 3</th>
      </tr>
      {% for value0, value1, value2 in content_pdf %}
      <tr>
         <td>{{ value0 }}</td>
         <td>{{ value1 }}</td>
         <td>{{ value2 }}</td>
      </tr>
      {% endfor %}
   </table>
</div>

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

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