Django模板对象属性查找与动态变量名称 [英] Django template object property lookup with a dynamic variable name

查看:325
本文介绍了Django模板对象属性查找与动态变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,有用户,等级,蹲下,强力,台阶,清洁和干劲。



排名系统会将任意数量的组合从只有总计蹲下到总量排列,并且排除到所有五个学科的总金额。您可以看到,用户可以通过以下方式检查排名的大量可用组合。虽然我还没有把所有这些排名组合添加到我的数据库中,我已经完成了主要的5,即SQ_rank。



在我的视图文件中,我已经完成了以下request.GET得到'SQ','DL','BP','Snatch'或'CJ':

  def排名()
a =''
for i in request.GET:
a + = i
b = a +'_rank'
query_results = UserProfile.objects.all ).order_by(a)
rank = b
return render_to_response('registration / rankings.html',{'query_results':query_results,'rank':rank,},context_instance = RequestContext = [custom_proc]))

现在我希望排名作为我的模板内的一个变量。它会返回一些_rank,如SQ_rank或DL_rank,这在我的数据库中。

  {%for query_results%} 
< tr>
< td> {{item.user}}< / td>
< td> {{items。??? }}< / TD>
< td> {{item.SQ}}< / td>
< td> {{item.DL}}< / td>
< td> {{item.BP}}< / td>
< td> {{item.CJ}}< / td>
< td> {{item.Snatch}}< / td>
< / tr>
{%endfor%}

如果没有堆载入{ %ifequal%}}标签占用了在我将其添加到我的数据库时可用的所有可变组合?所有这一切都有意义

解决方案

首先:

 为我的请求.GET:

因为request.GET无效是一个dict,而不是一个列表。如果你的意思是项目。是查找名称包含在rank变量中的item属性,这是我为此目的使用的:

  {{item | ofKey:rank}} 

ofKey是一个自定义模板过滤器。创建一个名为 extra.py 的文件,其中包含:

 从django导入模板
register = template.Library()

@ register.filter
def ofKey(value,arg):
如果值:
return value.get(arg)
else:
return

现在将此文件保存在您的一个应用程序目录的templatetags子目录中。您将需要使用:

  {%load extra%} 

在您需要的模板的开头。


I have a table with user, rank, squat, deadlift, benchpress, Clean and Jerk and Snatch.

The ranking system will rank any number of combinations from just total amount Squated, to total amount benchpressed and deadlifted to the total amount lifted across all five disciplines. As you can see there is a huge number of available combinations that a user can check their rankings by. While i have as yet to painstakingly add all these ranking combinations to my database i have done the main 5 ie SQ_rank.

In my views file i have done the following, where request.GET gets either 'SQ', 'DL', 'BP', 'Snatch' or 'CJ':

def rankings()
    a = ''
    for i in request.GET:
        a += i    
    b = a + '_rank'    
    query_results = UserProfile.objects.all().order_by(a)
    rank = b
    return render_to_response('registration/rankings.html',{'query_results': query_results, 'rank': rank,}, context_instance=RequestContext(request, processors=[custom_proc])) 

Now i want the rank as a variable inside of my template. it will return something _rank such as SQ_rank or DL_rank, which is in my database.

{% for item in query_results %}
    <tr>
    <td>{{ item.user }}</td>
    <td>{{ items.??? }}</td>
    <td>{{ item.SQ }}</td>
    <td>{{ item.DL }}</td>
    <td>{{ item.BP }}</td>
    <td>{{ item.CJ }}</td>
    <td>{{ item.Snatch }}</td>
    </tr>
{% endfor %}

How do i do this without having a heap load of {% ifequal %}} tags accounting for all the varaible combinations that will be available when i get around to adding them to my database? Does all this make sense

解决方案

First of all:

for i in request.GET:

is invalid since request.GET is a dict, not a list. If what you mean by item.??? is to lookup the item property whose name is contained in the "rank" variable, this is what I use for this purpose:

{{ item|ofKey:rank }}

The "ofKey" is a custom template filter. Create a file called, say, extra.py which contains:

from django import template
register = template.Library()

@register.filter
def ofKey(value, arg):
    if value:
        return value.get(arg)
    else:
        return ""

Now save this file in a "templatetags" subdirectory of one of your app directories. You will need to use:

{% load extra %}

at the beginning of the templates where you will need it.

这篇关于Django模板对象属性查找与动态变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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