0

我的代码中有此代码forms.py

from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

当我尝试以编程方式提取代码时,inspect它会遗漏fieldsets

In [1]: import inspect

In [2]: import forms

In [3]: print inspect.getsource(forms)
from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

In [4]: print inspect.getsource(forms.ContactForm)
class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)


In [5]:      

这似乎不是空行的问题。我已经在没有空行的情况下进行了测试,并且在其他属性之间添加了额外的空行。结果不变。

任何想法为什么检查只返回之前的部分fieldsets而不是整个课程的来源?

4

2 回答 2

1

编辑:根据评论修改:

inspect.getsource(forms.ContactForm)该方法内部BlockFinder.tokeneater()用于确定ContactForm块停止的位置。除此之外,它还会检查tokenize.DEDENT,它会在存储在 github 的版本中的字段集之前找到它。该行仅包含一个换行符,因此inspect认为当前块已结束。

如果你插入 4 个空格,它又对我有用。我无法争论这背后的理由,也许是性能。

class ContactForm(forms.Form):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)
    # <-- insert 4 spaces here
    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

工作方式不同的原因inspect.getsource(forms)是因为inspect在这种情况下不必确定类定义的开始和结束。它只是输出整个文件。

于 2009-05-30T10:47:17.170 回答
0

为我工作。我的代码中没有“from formfieldset.forms import FieldsetMixin”。也许这导致了一个问题..

于 2009-05-30T11:54:59.013 回答