在Django Template

时间:2017-03-19 15:19:38

标签: python django django-templates

形式:

{% for user in users %}
  <tr>
      <td>{{ user.teammembers }}</td>
      <td>{{ user.level }}</td>
      <td>{{ user.skillset }}</td>
      <td>{{ user.id }}</td>
      {% for dat in finaldatod %}
        <td>{{ user.dat }}</td>
      {% endfor %}
  </tr>
{% endfor %}

型号:

class Jan17(models.Model):
    id = models.TextField(primary_key=True)
    slno = models.TextField(db_column='SLNo')
    teammembers = models.TextField(db_column='TeamMembers')
    level = models.TextField(db_column='Level')
    skillset = models.TextField(db_column='Skillset')
    employeeid = models.TextField(db_column='EmployeeID')
    pridid = models.TextField(db_column='PridID')
    jan01 = models.TextField(db_column='Jan01')
    jan02 = models.TextField(db_column='Jan02')

查看:

def filter_users(request):
    form = UserlistForm(request.POST or None)
    users = None
    fromdate = None
    todate = None
    months = None
    finaldat = {}
    finaldatod = {}
    if request.method == 'POST':
        if form.is_valid():
            users = Jan17.objects.filter(Q(teammembers__contains=form.cleaned_data.get('teammembers')))
            fromdate = form.cleaned_data.get('fromdate')
            todate = form.cleaned_data.get('todate')
            months = form.cleaned_data.get('months')
            for dat in range(int(fromdate),int(todate)+1):
                if dat in [1,2,3,4,5,6,7,8,9]:
                    findat = months + '0' + str(dat)
                else:
                    findat = months+str(dat)
                finaldat[findat] = findat
                finaldatod = collections.OrderedDict(sorted(finaldat.items()))

    return(render(request, 'useroutput.html', {'form': form, 'users': users, 'fromdate': fromdate, 'todate': todate, 'months': months, 'finaldatod': finaldatod}))

模板:

<form action="." enctype="application/x-www-form-urlencoded" method="post">
{{ form.as_ul }}
{% csrf_token %}
<button type="submit">Search</button>

{% if users %}

<table border="5">
    <tr>
    <th>TeamMember</th>
    <th>Level</th>
    <th>SkillSet</th>
    <th>PridID</th>
    {% for dat in finaldatod %}
    <th>{{ dat }}</th>
    {% endfor %}
    </tr>
    {% for user in users %}
    <tr>
        <td>{{ user.teammembers }}</td>
        <td>{{ user.level }}</td>
        <td>{{ user.skillset }}</td>
        <td>{{ user.pridid }}</td>
        <td>{{ user.jan01 }}</td>
        <td>{{ user.jan02 }}</td>
        {% for dat in finaldatod %}
        <td>{{ user.dat }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>
{% endif %}

此处usersdat是从我看来传递的两个词典。

  • users拥有数据库中的所有用户
  • dat包含该月的所有日期

现在我可以获取所有用户的usernameskillsetlevelid。但无法获取该月所有日期的用户轮班。

此处{{ user.dat }}无效,因为它无法将user值和dat值替换为一起。

请告知我如何将值从第一个for循环传递到第二个循环并从数据库中获取数据。

提前致谢!!

0 个答案:

没有答案