简体   繁体   English

Django将模板变量传递给自定义templatetag

[英]Django pass template variable to custom templatetag

I'd like to display django my error messages inside the form as a placeholder, instead of above the form input box itself. 我想在表单内以占位符显示Django我的错误消息,而不是在表单输入框本身上方显示。 Is this possible in Django, or would I have to achieve this by writing some Javascript? 这在Django中是否可能,或者我必须通过编写一些Javascript来实现?

    <div class="form-group">

        {% if login_form.password.errors %}
            {% for error in login_form.password.errors %}
                <p>{{ error }}</p>
            {% endfor %}
        {% endif %}

        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
            {{ login_form.password | placeholder:"password" | addClass:"form-control" }}
        </div>

    </div>

Specific lines of interest. 特定的兴趣线。 I'd like to change this: 我想更改此:

{{ login_form.password | placeholder:"password" | addClass:"form-control" }}

To this 对此

{{ login_form.password | placeholder:{{ error }} | addClass:"form-control" }}

where {{error}} is an the Django error message thrown by the form 其中{{error}}是表单抛出的Django错误消息

I'm thinking there must be some way of getting the string value of a template variable, but can't find any answers 我在想必须有某种方法来获取模板变量的字符串值,但是找不到任何答案

EDIT: I've tried the following, to no avail 编辑:我尝试了以下,无济于事

<div class="form-group">
    {% if login_form.password.errors %}
        {% for error in login_form.password.errors %}
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
                {{ login_form.password | placeholder:{{ error|escape }} | addClass:"form-control" }}
            </div>
        {% endfor %}
    {% endif %}
</div>

My custom templatetag looks as follows 我的自定义templatetag如下所示

@register.filter(name='placeholder')
def placeholder(value, token):
    value.field.widget.attrs["placeholder"] = token
    return value

Django throws a TemplateSyntax Error complaining that: Django抛出TemplateSyntax错误,抱怨:

placeholder requires 2 arguments, 1 provided 占位符需要2个参数,提供1个

First, the nested expression in django template system is incorrect, so placeholder 's filter argument shouldn't be enclosed in double brackets, but written directly: 首先,django模板系统中的嵌套表达式不正确,因此不应将placeholder的filter参数括在双括号中,而应直接写成:

{{ login_form.password | placeholder:error | addClass:"form-control" }}

Here, error variable is correctly evaluated in context of outer curly brackets scope. 在此,可以在外部花括号范围的上下文中正确评估error变量。

Second, I'd suggest make sure about filter argument's type. 其次,我建议确保确定过滤器参数的类型。 If you do expect string value, you may use built-in filter to make an explicit conversion: 如果确实期望字符串值,则可以使用内置过滤器进行显式转换:

{% with err_msg=error|stringformat:"s" %}
{{ login_form.password | placeholder:err_msg | addClass:"form-control" }}
{% endwith %}

This can be done. 可以做到的。

{{ login_form.password.errors }}

Here's the relevant documentation for that https://docs.djangoproject.com/en/1.9/topics/forms/#rendering-form-error-messages 这是该https://docs.djangoproject.com/zh-CN/1.9/topics/forms/#rendering-form-error-messages的相关文档

The piece of code should be re-written as 该段代码应重写为

{{ login_form.password | placeholder:"password" | addClass:"form-control" }}
{% if login_form.password.errors %}
   <span class="error">{{ login_form.password.errors }}</span>
{% endif %}

or can be done in this way too 或者也可以这样

{{ login_form.password | placeholder:"password" | addClass:"form-control" }}
<input type="password" class="form-control" placeholder="
{% if login_form.password.errors %}
   {{ login_form.password.errors }}
{% else %}
   password
{% endif %}
">

I have not tested yet, but this may work: 我尚未测试,但这可能有效:

# forms.py
class MyForm(forms.Form):
    class meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        error_messages = self.fields['myfield'].error_messages
        self.fields['myfield'].widget.attrs.update({'placeholder': error_messages})

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

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