简体   繁体   English

Django - 如何使用带有多个参数的templatetags过滤器

[英]Django — how to use templatetags filter with multiple arguments

I have a few values that I would like to pass into a filter and get a URL out of it. 我有一些值,我想传递给过滤器,并从中获取一个URL。

In my template I have: 在我的模板中,我有:

{% if names %}
  {% for name in names %}
    <a href='{{name|slugify|add_args:"custid=name.id, sortid=2"}}'>{{name}}</a>
    {%if not forloop.last %} | {% endif %}
  {% endfor %}
{% endif %}

In my templatetags I have: 在我的模板标签中,我有:

@register.filter
def add_args(value, args):
    argz = value.strip() + '-' + 'ARGS'
    arglist = args.split(',')
    for arg in arglist:
        keyval = arg.split('=')
        argz.join(keyval[0] + 'ZZ' + keyval[1])
        argz.join('QQ')

    return argz  

The output URL should look like: 输出URL应如下所示:

http://foo.org/john-smith-ARGScustidZZ11QQsortidZZ2

Where ARGS is the start of the arguments, ZZ is '=' and QQ is an '&' equivalent. 如果ARGS是参数的开头,则ZZ为'=',QQ为'&'等价物。

First of all: This would work, but I get the custid=name.id coming in the add_args(), where I want to have custid=11 to come in. How pass in the id as an id and not text. 首先:这会有效,但我得到了add_args()中的custid = name.id,我希望custid = 11进来。如何将id作为id传入而不是文本。

Also, is there a way to just send in an array of key=>value like in PHP. 另外,有没有办法像PHP一样发送key => value数组。 In PHP I would build an array, let say: 在PHP中我会构建一个数组,让我们说:

arglist = array('custid' => $nameid, 'sortid' => $sortid ); 

Then I would pass the arglist as an argument to add_args() and in add_args() I would do 然后我将arglist作为参数传递给add_args()并在add_args()add_args()

foreach( arglist as $key => $value)
  $argstr .= $key . 'ZZ' . $value . 'QQ'.

Does anyone have a better way of making this work? 有没有人有更好的方法来完成这项工作?

Note: if I have to pass all arguments as a string and split them up in the filter I don't mind. 注意:如果我必须将所有参数作为字符串传递并在过滤器中将它们拆分,我不介意。 I just don't know how to pass the name.id as its value ... 我只是不知道如何传递name.id作为其价值......

This "smart" stuff logic should not be in the template. 这种“智能”的东西逻辑不应该在模板中。 Build your end-of-urls in your view and then pass them to template: 在您的视图中构建您的URL结尾,然后将它们传递给模板:

def the_view(request):
  url_stuff = "custid=%s, sortid, ...." % (name.id, 2 ...)

  return render_to_response('template.html',
    {'url_stuff':url_stuff,},
    context_instance = RequestContext(request))

In template.html: 在template.html中:

 ....

    <a href='{{url_stuff}}'>{{name}}</a>

 ....

If you need a url for a whole bunch of objects consider using get_absolute_url on the model. 如果您需要一大堆对象的URL,请考虑在模型上使用get_absolute_url

You can't pass name.id to your filter. 您无法将name.id传递给过滤器。 Filter arguments can be asingle value or a single literal. 过滤器参数可以是单个值或单个文字。 Python/Django doesn't attempt any "smart" variable replacement like PHP. Python / Django不会像PHP那样尝试任何“智能”变量替换。

I suggest you to create a tag for this task: 我建议你为这个任务创建一个标签:

<a href='{% add_args "custid" name.id "sortid" "2" %}{{name|slugify}}{% end_add_args %}'>{{name}}</a>

This way you can know which argument is a literal value and which should be taken fron context etc... Docs are quite clear about this, take a look at the example . 通过这种方式,您可以知道哪个参数是文字值,哪个参数应该从上下文等处获取...文档很清楚这一点,看看这个例子

Also if this name is any way related to a model, say we want to get to the permalink, adding a method that returns the URL with the proper arguments might be the tidiest solution. 此外,如果此name与模型有任何关联,请说我们想要进入永久链接,添加一个返回带有正确参数的URL的方法可能是最整洁的解决方案。

Overall, I would refrain putting too much logic into templates. 总的来说,我会避免在模板中加入过多的逻辑。 Django is not PHP. Django不是PHP。

You're calling argz.join a couple times and never assigning the results to anything: maybe you're operating under the misconception that the join method of a string has some mysterious side effect, but it doesn't -- it just returns a new string, and if you don't do anything with that new string, poof , it's gone. 你正在调用argz.join几次并且从不将结果分配给任何东西:也许你在错误的观念下操作,即字符串的join方法有一些神秘的副作用,但它没有 - 它只返回一个新的字符串,如果你没有对那个新的字符串做什么, poof ,它已经消失了。 Is that at least part of your problem...? 这至少是你问题的一部分......?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM