在视图中循环 [英] For loop in views

查看:52
本文介绍了在视图中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图中,我想获取汽车对象,然后更改 first_year var.

In views I want to get car-object, then change first_year var.

def cars(request, mark_id, year=year):
        #BMW, etc.
    mark = get_object_or_404(Mark, pk=mark_id, active=1)
        #M, X-series, etc.
    for group in mark.groups.iterator():
        group.first_year.year = int(year)-int(group.first_year.year)
    return render(request, 'cars.html', {  'mark':mark, })

因此,在模板中,我使用:

So, in template I use:

{% for i in mark.groups.all %}

{{i.first_year}}

{% endfor %}

它从db返回值,而不是我的 group.first_year.year .如您所知,有3种模型-标记,组和first_year.如果您需要,我可以在此处发布它们,但是我认为这是我的观点有误.

And it returns the values from db, not my group.first_year.year. As you can understand, there is 3 models - mark, groups and first_year. If you need, I can publish they here, but I think, that something wrong in my views.

谢谢.

编辑.型号.

class First_Year(models.Model):
    year = models.IntegerField()
    def __unicode__(self):
        return str(self.year)

class Groups(models.Model):
        many_other_fields
    mark = models.ForeignKey(Mark, related_name='groups')
    last_update = models.DateTimeField()
    first_year = models.ForeignKey(First_Year, related_name='first_year')
    def __unicode__(self):
        return self.name
    def altered_date(self, year):
        altered_year = int(year)-int(self.first_year.year)
        return altered_year

没有模型Mark,因为它有许多没有年份等的字段.

Without model Mark, bacause it has many-many fields without year and etc.

推荐答案

在视图中进行更改的 for 循环不会将更改保存到任何位置,因此在传递值时您将看不到更改到您的模板.

The for loop making changes in your view is not saving the changes anywhere so you will not see the changes when the values are passed to your template.

这里的一种解决方案是在模型中添加新的模型方法,并在那里进行日期比较.

One solution here is to add a new model method to your model and do the date comparison there.

在您的 First_Year 模型中,添加 altered_date 函数,如下所示:

In your First_Year model add the altered_date function like so:

class First_Year(models.Model):
    year = models.IntegerField()
    def __unicode__(self):
        return str(self.year)
    def altered_date(self, this_year):
        altered_year = int(this_year)-int(self.year)
        return altered_year

这将通过调用函数获取每个 First_Year 模型的更改年份.不幸的是,此函数需要一个附加参数- year -因此无法直接从模板中调用它.您可以创建一个自定义模板过滤器来解决此问题:

This gets the altered year for every First_Year model by calling the function. Unfortunately this function requires an additional parameter - year - so it cannot be called directly from the template. You can create a custom template filter to get around this:

@register.filter
def get_altered_year(obj, gar):
    return obj.altered_date(gar)

现在,您只需要通过像这样修改收益就可以确保将年份传递给视图:

Now you just need to make sure that you pass year to your view by modifying your return like so:

def cars(request, mark_id, year=year):
    ...
    return render(request, 'cars.html', {'mark':mark, 'year':year, })

然后在您的模板中可以执行以下操作:

And then in your template you can do:

{% for i in mark.groups_set.all %}
    {{i.first_year|get_altered_year:year }}
{% endfor %}

您可以在此处查看模型方法文档一个>.以及自定义模板过滤器的文档此处.

You can look at the model method documentation here. And the documentation for custom template filters here.

这篇关于在视图中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆