質問

私は私の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前に一部だけを返すとされていない?

役に立ちましたか?

解決

編集:コメントに基づいて改訂:

inspect.getsource(forms.ContactForm)の内部メソッドBlockFinder.tokeneater()ContactFormブロックが停止する場所を決定するために使用されます。他の人のほかに、それは右のgithubのに保存されているバージョンでfieldsetの前に見つかっtokenize.DEDENT、チェックします。行は改行が含まれているので、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理由があります。これは単に全体のファイルを出力します。

他のヒント

私のために動作します。私のコードで「formfieldset.formsはFieldsetMixinをインポートから」私は持っていません。たぶんそれが問題を引き起こしている..

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top