如何在Django模板表中使用ifchanged和rowpan嵌套的重新组合

时间:2019-02-07 04:27:04

标签: django python-3.x templates

我的源代码: https://gist.github.com/happychallenge/8c5e994f9e4d5c39fa51f5fe3266c32d

在表的嵌套重组中,ifchanged无法正常工作。

我想按成本类型和货币对业务成本进行分类。 如果我对cost_type进行分类,则它可以正常工作。 但是我将业务成本按cost_type和currency进行分类,表格一团糟。 如何修改下面的代码。 参考图片 categorize only cost_type cost_type and currency

models.py

class Service(models.Model):
    REWORK = '재작업'
    CUSTOMER = '고객요청'
    MAINTENANCE = '하자보완'
    ADDITIONAL = '추가작업'

    ASTYPE = (
        (MAINTENANCE, '하자보완'),
        (REWORK, '재작업'),
        (CUSTOMER, '고객요청'),
        (ADDITIONAL, '추가작업'),
    )

    ACCEPT = 'AS 접수'
    TEAMASSIGNED = '팀 할당'
    SITE_COMPLETE = '현장 완료'
    REPORT = '출장 보고서 작성'
    COMPLETE = 'AS 처리 완료'
    STYPE = (
        (ACCEPT, 'AS 접수'),
        (TEAMASSIGNED, '팀 할당'),
        (SITE_COMPLETE, '현장 완료'),
        (REPORT, '출장 보고서 작성'),
        (COMPLETE, 'AS 처리 완료'),
    )

    ascode = models.CharField("AS 코드", max_length=10, unique=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, 
                on_delete=models.SET_NULL,null=True)
    customer = models.ForeignKey(CustCompany, on_delete=models.SET_NULL,
                blank=True, null=True, verbose_name='고객사 명')
    project_name = models.CharField("Project 명", max_length=100, default='')
    representative = models.ForeignKey(Representative, on_delete=models.CASCADE,
                blank=True, null=True, verbose_name='고객 담당자')

    asmans = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='asmans')

    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-created_at',)

    def __str__(self):
        return self.ascode

    def get_absolute_url(self):
        return reverse('fta:service_detail', args=[self.id])

    def get_cost_count(self):
        return self.costs.count()

    def get_costs(self):
        return self.costs.all().order_by('ctype', 'currency')


class Cost(models.Model):
    TRAFFIC = 'TRANSPORTATION'
    FOOD = 'FOOD'
    HOTEL = 'HOTEL'
    BUSINESS = 'TRIP'
    ETC = 'ETC'
    CTYPE = (
        (TRAFFIC, 'TRANSPORTATION'),
        (FOOD, 'FOOD'),
        (HOTEL, 'HOTEL'),
        (BUSINESS, 'TRIP'),
        (ETC, 'ETC'),
    )

    KOREAN = 'KRW'
    RMB = 'CHINA¥'
    JPN = 'JAPAN¥'
    DOLLAR = 'DOLLAR'
    CURRENCY = (
        (KOREAN, 'KRW'),
        (RMB, 'CHINA¥'),
        (JPN, 'JAPAN¥'),
        (DOLLAR, 'DOLLAR'),
    )
    service = models.ForeignKey(Service, on_delete=models.CASCADE,
                related_name='costs')
    ctype = models.CharField("비용 구분", max_length=50, 
                choices=CTYPE, default=TRAFFIC)
    content = models.CharField("사용 내역", max_length=100, 
                blank=True, null=True)
    currency = models.CharField("환율", max_length=10,
                choices=CURRENCY, default=KOREAN)
    cost = models.PositiveIntegerField()
    author = models.ForeignKey(settings.AUTH_USER_MODEL, 
                on_delete=models.CASCADE,null=True)

cost_list.html

{% load humanize %}

<table class="table table-striped table-bordered table-hover">
<thead>
    <tr>
        <th width="20%">COST TYPE</th>
        <th width="15%">CURRENCY</th>
        <th width="45%">CONTENT</th>
        <th width="20%">AMOUNT</th>
    </tr>
</thead>
<tbody>
{% regroup service.get_costs by ctype as ctypes %}
{% for ctype in ctypes %}
    {% for cost in ctype.list %}
        <tr>
            {% ifchanged ctype %}
                <td rowspan="{{ ctype.list|length }}"> {{ctype.grouper}} </td>
            {% endifchanged %}

            <td> {{cost.currency}} </td>
            <td> 
                {{cost.content }} 
                {% if cost.author == user %}
                    <button class="btn btn-warning btn-xs pull-right" id="btn-cost-edit"
                    data-url="{% url 'fta:service_cost_edit' cost.id %}" 
                    >edit</button>
                {% endif %}
            </td>
            <td align="right"> {{cost.cost|intcomma}} </td>
        </tr>
    {% endfor %}
{% endfor %}
</tbody>

cost_list2.html

{% load humanize %}

<table class="table table-striped table-bordered table-hover">
<thead>
    <tr>
        <th width="20%">COST TYPE</th>
        <th width="15%">CURRENCY</th>
        <th width="45%">CONTENT</th>
        <th width="20%">AMOUNT</th>
    </tr>
</thead>
<tbody>
{% regroup service.get_costs by ctype as costtypes %}
{% for costtype in costtypes %}

    {% regroup costtype.list by currency as monytypes %}
    {% for money in monytypes %}
        {% for cost in money.list %}
            <tr>
                cost_type : {{ costtype.grouper }} <br>
                {% ifchanged costtype.grouper %}
                    <td rowspan="{{ costtype.list|length }}"> {{costtype.grouper}} </td>
                {% endifchanged %}
                &nbsp;&nbsp;&nbsp; currency {{money.grouper}} <br>
                {% ifchanged money.grouper %}
                    <td rowspan="{{ money.list|length }}"> {{money.grouper}} </td>
                {% endifchanged %}
                <td> 
                    {{cost.content }} 
                    {% if cost.author == user %}
                        <button class="btn btn-warning btn-xs pull-right" id="btn-cost-edit"
                        data-url="{% url 'fta:service_cost_edit' cost.id %}" 
                        >edit</button>
                    {% endif %}
                </td>
                <td align="right"> {{cost.cost|intcomma}} </td>
            </tr>
        {% endfor %}
    {% endfor %}
{% endfor %}
</tbody>

0 个答案:

没有答案