对于同一循环中两个对象的循环迭代

时间:2019-05-09 07:40:35

标签: django postgresql django-models django-forms django-templates

我正在使用models.py提交一个具有不同属性(例如月份和年份)的文件,当用户经过月份和年份时,它应该与数据库中的对象匹配并返回文件。我将所有文件附加在字典中,然后将其返回到HTML。但是它没有显示结果

#views.py

from django.shortcuts import render
from .models import Results


def allresults(request):
    results = Results.objects.all()
    mon = request.POST['mahina']
    yea = request.POST['saal']

    if (mon == 'all'):
        pdf_url = {}

        for i in results:
            m = i.month
            y = i.year
            f = i.file
            u = f.url
            if (yea == y):
                pdf_url[f] = u
    pl = pdf_url.items()
    return render(request, 'results/allresults.html', {'y': yea, 'pl': pl})
#models.py
from django.db import models


class Results(models.Model):
    date = models.CharField(max_length=255)
    title = models.CharField(max_length=255)
    month = models.CharField(max_length=22)
    year = models.IntegerField()
    file = models.FileField(upload_to='file/')

    def __str__(self):
        return self.date
{% for i,j in pl %}

            <strong>@  result.date </strong><span> result.title</span>
<span style="float: right;"><a href="{{ j }}"><img src="{% static 'pdf.jpg'%}"></a></span>
          </p>
        </div>
        {% endfor %}

0 个答案:

没有答案