在窗口小部件级别检查错误和其他值-可能使用自定义表单字段 [英] Check for errors and other values at the widget level - maybe using custom form field

查看:101
本文介绍了在窗口小部件级别检查错误和其他值-可能使用自定义表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果字段在小部件级别有错误,我该如何访问?

How can I access if a field has)errors at the level of widget?

我尝试使用默认设置:

{% if widget.attributes.has_errors %} or {% if widget.has_errors %}

但不起作用.

我使用自定义窗口小部件模板,我正在考虑使用自定义表单字段并覆盖默认字段.

I use custom widget templates, I'm thinking to use a custom form Field and overwrite the default field.

我知道clean方法存在,但是我不知道如何将所需的动态(非默认)数据/属性推送到小部件.

I know clean method exist but I don't know how to push to the widget the dynamic(non default) data/attributes I want.

我尝试过:

class AWidget(forms.Widget):

    def get_context(self, name, value, attrs):

        context = super().get_context(name, value, attrs)
        has_errors = context['widget']['attrs'].pop('has_errors', None)
         context['widget']['has_errors'] = has_errors

它适用于errors,但是我不知道这是否是最好的选择,而且我想从Form Field传递其他值/属性,我认为尝试覆盖Form Field会更好,但是我不愿意不知道怎么做.

It works for errors but I don't know if is the best option plus I want to pass other values/attributes from Form Field,and I think will be better to try to overwrite the Form Field but I don't know exactly how.

还可以使用以下方法访问各个属性:

Also accessing individual attributes using:

 {{ widget.attrs.maxlength }} or  {{ widget.attrs.items.maxlength }}

即使加入for循环也可以

even if accedes in a for loop works

我知道我可以添加带有一类错误的父div:

I know I can add a parent div with a class of error:

 <div class="{% if form.field.errors %}pass_error{% endif %}">
        {{ form.field }} 
    </div>

但是,这意味着在css级别有很大的变化.

but, that implies big changes at the css level.

我已经用自定义小部件覆盖了所有Django小部件,因为错误,我不需要只是更改边框颜色,而是显示或不显示小部件模板的不同元素以及某些位置他们改变了.

I already overwrite all Django widgets with custom widgets, on error I don't need just to change a border color, but to show or not different elements of the widget template and the position of some of them change.

我已经修改了基于基础的窗口小部件以添加错误,但是我希望通过从字段到窗口小部件的传递(取决于错误类型)来以一种更优雅的方式在字段级别上做到这一点.

I already modify the based widget to add errors, but I'm looking to do it in a more elegant way at the field level by passing from the field to the widget, parameters depending on error type.

所以我的问题是我需要重写什么才能从字段传递到小部件错误和其他变量?

So my question is what I need to overwrite to pass from field to widget errors and other variables ?

推荐答案

不确定这是否对您的特定用例有所帮助...但是以防万一,请注意,在视图中构建表单时,可以根据需要添加其他参数,然后将其传递给您的自定义小部件.

Not sure whether this could help in your specific use case ... but just in case, please note that when you build your form in the view, you can add extra parameters as needed, then pass them down to your custom widget.

工作示例:

文件"forms.py"

file "forms.py"

from django import forms


def build_ingredient_form(unit):
    """
    Ingredient form factory

    Here we build the form class dynamically, in order to acces 'unit' via closure.
    References:
        http://stackoverflow.com/questions/622982/django-passing-custom-form-parameters-to-formset#623030
    """
    class IngredientForm(forms.Form):
        #quantity = forms.DecimalField(max_digits=10)
        quantity = UnitField(unit, required=False)
        ...
    return IngredientForm


文件"fields.py"

file "fields.py"

from django import forms
from .fields import UnitField


class UnitField(forms.CharField):
    """
    Custom field to support UnitWidget

    References:
        - http://tothinkornottothink.com/post/10815277049/django-forms-i-custom-fields-and-widgets-in
    """

    def __init__(self, unit, *args, **kwargs):
        self.unit = unit
        super(UnitField, self).__init__(*args, **kwargs)
        self.widget = UnitWidget(unit)

    ...

文件"widgets.py"

file "widgets.py"

from django import forms
from .models import Unit


class UnitWidget(forms.TextInput):

    def __init__(self, unit, attrs=None):
        if unit is None:
            self.unit = Unit()
        else:
            self.unit = unit

    ...

这篇关于在窗口小部件级别检查错误和其他值-可能使用自定义表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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