如何使用 <div class='field_type'> 标记表单字段在姜戈 [英] How to markup form fields with &lt;div class=&#39;field_type&#39;&gt; in Django

查看:34
本文介绍了如何使用 <div class='field_type'> 标记表单字段在姜戈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一种方法来识别 django 模板中的字段类型.我的解决方案是创建一个简单的过滤器来访问字段和小部件类名称.我已经包含了下面的代码,以防对其他人有帮助.

有更好的方法吗?

## agency/tagutils/templatetags/fieldtags.py#############################################################从 Django 导入模板注册 = 模板.图书馆()@register.filter(name='field_type')定义字段类型(值):返回值.field.__class__.__name__@register.filter(name='widget_type')定义小部件类型(值):返回值.field.widget.__class__.__name__## 客户端/项目/settings.py#############################################################安装_应用程序 = (# ...'agency.tagutils',)## 客户端/项目/模板/项目/field_snippet.html#############################################################{% 加载 fieldtags %}<div class="field {{ field|field_type }} {{ field|widget_type }} {{ field.name }}">{{ field.errors }}<div class="form_label">{{ field.label_tag }}

<div class="form_field">{{ 场地 }}

##示例输出html#############################################################<div class="field CharField TextInput family_name"><div class="form_label"><label for="id_family_name">姓</label>

<div class="form_field"><input id="id_family_name" type="text" name="family_name" maxlength="64"/>

解决方案

class MyForm(forms.Form):myfield = forms.CharField(widget=forms.TextInput(attrs={'class' : 'myfieldclass'}))

或者,使用 ModelForm

class MyForm(forms.ModelForm):元类:模型 = 我的模型小部件 = {'myfield': forms.TextInput(attrs={'class': 'myfieldclass'}),}

或者,当您不想重新定义小部件时

class MyForm(forms.ModelForm):元类:模型 = 我的模型def __init__(self, *args, **kwargs):super(MyForm, self).__init__(*args, **kwargs)self.fields['myfield'].widget.attrs.update({'class' : 'myfieldclass'})

用{{ form }}正常渲染

I wasn't able to find a way to identify the type of a field in a django template. My solution was to create a simple filter to access the field and widget class names. I've included the code below in case it's helpful for someone else.

Is there a better approach?

## agency/tagutils/templatetags/fieldtags.py
###############################################################
from django import template

register = template.Library()

@register.filter(name='field_type')
def field_type(value):
    return value.field.__class__.__name__

@register.filter(name='widget_type')
def widget_type(value):
    return value.field.widget.__class__.__name__


## client/project/settings.py
###############################################################

INSTALLED_APPS = (
    # ...
    'agency.tagutils',
)


## client/project/templates/project/field_snippet.html
###############################################################

{% load fieldtags %}

<div class="field {{ field|field_type }} {{ field|widget_type }} {{ field.name }}">
     {{ field.errors }}
    <div class="form_label">
        {{ field.label_tag }}
    </div>
    <div class="form_field">
    {{ field }}
    </div>
</div>


## sample output html
###############################################################
<div class="field CharField TextInput family_name">    
    <div class="form_label">
        <label for="id_family_name">Family name</label>
    </div>
    <div class="form_field">
    <input id="id_family_name" type="text" name="family_name" maxlength="64" />
    </div>
</div>

解决方案

class MyForm(forms.Form):
    myfield = forms.CharField(widget=forms.TextInput(attrs={'class' : 'myfieldclass'}))

or, with a ModelForm

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        widgets = {
            'myfield': forms.TextInput(attrs={'class': 'myfieldclass'}),
        }

or, when you don't want to redefine the widget

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

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

render normally with {{ form }}

这篇关于如何使用 <div class='field_type'> 标记表单字段在姜戈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆