在Django视图中将表单字段作为属性访问 [英] Accessing form fields as properties in a django view

查看:53
本文介绍了在Django视图中将表单字段作为属性访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Django教程,您应该使用cleaned_data字典访问表单字段。我想知道为什么我不能直接访问表单的属性?我的表单可以很好地验证,但是当我尝试访问它时,Django抱怨对象没有属性。我在下面添加了一些代码,希望可以帮助诊断问题。

According to the Django tutorial, you should access form fields using cleaned_data dictionary. I'm wondering why I can't access the properties of the form directly? My form validates just fine, but when I try to access it, Django complains that the object does not have the attribute. I added some code below that I hope will help diagnose the problem.

表格:

class CustomForm(forms.Form):
    description = forms.CharField(widget = forms.TextInput(attrs = {'placeholder' : 'enter some text'}), label = "My form")

查看:

def process_form(request):
    if request.method != 'POST':
        raise Http404

    myForm = CustomForm(request.POST)

    if not myForm.is_valid():
        c = RequestContext(request)
        return render_to_response('home/index.html', {'form' : myForm }, c)

    # debug
    print 'Description: ' + myForm.description # this does NOT work
    # print 'Description: ' + myForm.cleaned_data['description'] # this does work

我得到以下错误:'CustomForm'对象没有属性'description'。我是否错过了文档中说我无法做到的事情?

I get the following error: 'CustomForm' object has no attribute 'description'. Did I miss something in the docs that says I can't do that?

推荐答案

使用<$ c定义字段的方式$ c> django.forms 只是一种方便的声明性语法。

The way you define fields using django.forms is just a convenient, declarative syntax; it's not really representative of what the final Form class, or an instance of it, looks like in terms of attributes.

表单的metaclass (不必太深入地了解,元类就是使用声明一个类class 作为 __ init __ 方法的关键字是使用括号创建类的实例-用于自定义所创建对象的钩子,元类的实例是!),它在定义时从表单类中提取字段并将其添加到 base_fields 字典。实例化表单时,其 base_fields 将被深度复制到实例上的 fields 属性。

Forms have a metaclass (without getting too deep into it, a metaclass is to declaring a class using the class keyword as an __init__ method is to creating an instance of a class using parentheses -- a hook to customise the object being created, which in the case of a metaclass, is a class!) which picks off Fields from the form class at definition time and adds them to a base_fields dict. When you instantiate a form, its base_fields are deep-copied to a fields attribute on the instance.

一个令人困惑的地方可能是您使用来访问模板中显示的字段-实际发生的是Django的模板引擎首先尝试使用字典样式的 [] 访问权限来解析属性查找,并且基本表单类定义了 __ getitem __ 方法可利用此优势,从表格中查找适当的字段实例的 fields 字典,并用 BoundField 包裹起来,这是一个包装器,它知道如何使用表单中的字段和数据用于显示字段。

One point of confusion might be that you use . to access fields for display in templates -- what's actually happening there is that Django's template engine first attempts to use dictionary-style [] access to resolve property lookups and the base form class defines a __getitem__ method to take advantage of this, looking up the appropriate field from the form instance's fields dict and wrapping it with a BoundField, a wrapper which knows how to use the field and data from the form for displaying the field.

这篇关于在Django视图中将表单字段作为属性访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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