如何在Django中将同一类别的所有费用分组?

克里斯·杰普

我是Django的新手,它尝试将同一类别的所有费用归为一组并进行检索,并在一个“ link like”表raw中进行检索,当单击该表格时,可以以另一种形式显示该类别中的所有费用。

我有这些模型:

class Category(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        verbose_name_plural = 'Categories'
        unique_together = ("name",)

    def __str__(self):
        return self.name


class Expense(models.Model):
    amount = models.FloatField()
    date = models.DateField(default=now, )
    description = models.TextField()
    owner = models.ForeignKey(to=User, on_delete=models.CASCADE)
    category = models.CharField(max_length=50)

    def __str__(self):
        return self.category

    class Meta:
        ordering = ['-date', '-pk']

首页查看:

def home(request):
    categories = Category.objects.all()
    expenses = Expense.objects.filter(owner=request.user)
    paginator = Paginator(expenses, 5)
    page_number = request.GET.get('page')
    page_obj = Paginator.get_page(paginator, page_number)
    currency = UserPreference.objects.filter(user=request.user)  # .currency
    query = Expense.objects.values('category').annotate(total=Sum(
                                    'amount')).order_by('category')
    context = {
        'expenses': expenses,
        'page_obj': page_obj,
        'currency': currency,
        'query': query
    }

    return render(request, 'expenses/home.html', context)

我的模板:

<div class="app-table"> 
  <table class="table table-stripped table-hover">
    <thead>
      <tr>
        <th>Amount</th>
        <th>Category</th>
        <th>Description</th>
        <th>Date</th>
        <th></th>
      </tr>
    </thead>
    
    <tbody>
    {% for myquery in query %}
      <tr class="clickable-row" data-href="https://www.mavtechplanet.com/">
        <td>{{myquery.category }}</td>
      </tr>
    
    {% endfor %}
    </tbody>
    
  </table>
</div>

我试图弄清楚事情,但不清楚。非常感谢您的帮助。

奥斯曼·杜尔达格(Osman Durdag)

您的模板:

{% for myquery in query %}
     <tr class="clickable-row" data-href="https://www.mavtechplanet.com/">
        <td><a href = "{% url 'yourapp:showCategoryExpenses' myquery.category %}">{{myquery.category}}</a></td>
     </tr>

{% endfor %}

将url添加到urls.py中,在您的应用中像这样,


from youraapp.views import showCategoryExpenses

urlpatterns=[
.
.
.
path("/categoryExpenses/<pk:categoryPk>", showCategoryExpenses, name = "showCategoryExpenses"),
# if you save categories to expense with their name, you must change the <pk:categoryPk> to <slug:categoryName>
]

并将此视图添加到yourapp.views中,如下所示:

def showCategoryExpenses(request, categoryPk): # if you save categories to expense with their name, change categoryPk to categoryName
    expenses = Expense.objects.filter(category = categoryPk) # or category = categoryName
    context = {
        "expenses" : expenses
    }
    return render(request, "expenses/categoryExpenses.html", context)

最后,将此模板添加到您的模板文件夹中。我的英语能力不好,但是我尝试解决并解释它。也许这就是您要寻找的答案。

如果要显示此模板中类别中所有费用的总和,可以执行以下操作:

def showCategoryExpenses(request, categoryPk): # if you save categories to expense with their name, change categoryPk to categoryName
    expenses = Expense.objects.filter(category = categoryPk) # or category = categoryName
    expensesSum = 0
    for expense in expenses:
        expensesSum += expense.amount
    context = {
        "expenses" : expenses,
        "expensesSum" : expensesSum
    }
    return render(request, "expenses/categoryExpenses.html", context)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Django中获取同一类别中的所有帖子

如何在 django 中创建来自同一类别的帖子列表

删除&nbsp; 来自同一类别的所有元素

如何显示同一类别的多个帖子?

Pandas 对列进行排序,但将同一类别的元素放在一起

打印同一类别的3个对象

测量同一类别的元素的总高度

创建同一类别的多个按钮

属于同一类别的多个列上的OneHotEncoder

熊猫:如何(干净)取消对同一类别的两列的透视?

CNN会针对所有输入数据预测同一类别

如何将同一作者的所有提交分组为一行?

在属于同一类别的单个列中获取多个图像时如何使图像并排对齐

在多个文件中将同一类的函数分开

选择一个类别的所有后代,但另一类别中的所有后代(反之亦然)

使用JavaScript,如何将同一文本写入多个HTML元素,或者如何将文本写入同一类的所有HTML元素?

下拉选项仅显示同一类别的选项中的一个

我想计算属于同一类别的那些产品的数量

加载更多同一类别的帖子-WordPress + Ajax + Timber

共享同一类别的两个自定义帖子类型

绘制属于同一类别的散点的平均值

Woocommerce 产品简短描述中同一类别的产品下拉菜单

Django中有2个类别的一种模型。如何在html中显示同一模型的另一个类别?

如何求和属于同一类的所有对象的参数之一

如何在同一列中将共享至少一个“ 1”的所有标签(索引)分组?

如何从jquery中属于同一类的成员的元素中获取所有不同的值?

如何为同一类别但英寸略有不同的布局定义两个不同的布局?

Ajax:为同一类别的多个项目之一发送textarea的值

如何使用 Spring Framework 将同一类的 2 个实例注入另外 2 个不同的类?