0

私はテンプレートを使用してデータを収集することに慣れていますが、表示時にDjangoがフィールドを表示し、適切な値を入力するスマートな方法があります。もちろん手動で行うこともできますが、モデルはフィールドタイプを認識しています。これに関するドキュメントはありませんでした。たとえば、次のテンプレートからデータを収集します。

   <strong>Company Name</strong>
   <font color="red">{{ form.companyname.errors }}</font>
   {{ form.companyname }}

ここで、formは、すべてのフィールドを含む私の会社のモデルです。Djangoがテキストフィールドをレンダリングして現在の値を入力するように、このタイプの方法論を使用できるようにするにはどうすればよいでしょうか。たとえば、次の方法で値を送信する方法があります。

    myid = int(self.request.get('id'))
    myrecord = Company.get_by_id(myid)
    category_list = CompanyCategory.all()
    path = os.path.join(os.path.dirname(__file__), 'editcompany.html')
    self.response.out.write(template.render(path, {'form': myrecord, 'category_list': category_list}))

レコードでこれを行うことはできますか?テンプレートには送信された値が入力されますか?ありがとう

4

2 に答える 2

3

Formvsの違いと適切な使い方について混乱しているようですModelForm

使用するフォームのタイプに関係なく、フォームのテンプレート側は同じままです。 注: フォーム内のすべての値 (POST にバインドされているか、インスタンスがある限り) は、レンダリング時に事前入力されます。

<form class="well" action="{% url member-profile %}" method="POST" enctype="multipart/form-data">{% csrf_token %}
    <fieldset>
        {{ form.non_field_errors }}

        {{ form.display_name.label_tag }}
        <span class="help-block">{{ form.display_name.help_text }}</span>
        {{ form.display_name }}
        <span class="error">{{ form.display_name.errors }}</span>

        {{ form.biography.label_tag }}
        <span class="help-block">{{ form.biography.help_text }}</span>
        {{ form.biography }}
        <span class="error">{{ form.biography.errors }}</span>

        <input type="submit" class="button primary" value="Save" />
    </fieldset>
</form>

レコードからフォームに入力する (またはフォームをレコードとして送信する) 場合は、おそらく使用するのが最善ですModelForm

EX ユーザー FK ドロップダウンを表示しないプロファイル フォーム:

class ProfileForm(forms.ModelForm):
    """Profile form"""      
    class Meta:
        model = Profile
        exclude = ('user',)

景色:

def profile(request):
    """Manage Account"""
    if request.user.is_anonymous() :
        # user isn't logged in
        messages.info(request, _(u'You are not logged in!'))
        return redirect('member-login')

    # get the currently logged in user's profile
    profile = request.user.profile

    # check to see if this request is a post
    if request.method == "POST":
        # Bind the post to the form w/ profile as initial
        form = ProfileForm(request.POST, instance=profile)
        if form.is_valid() :
            # if the form is valid
            form.save()
            messages.success(request, _(u'Success! You have updated your profile.'))
        else :
            # if the form is invalid
            messages.error(request, _(u'Error! Correct all errors in the form below and resubmit.'))
    else:
        # set the initial form values to the current user's profile's values
        form = ProfileForm(instance=profile)

    return render(
        request, 
        'membership/manage/profile.html', 
        {
            'form': form, 
        }
    )

else外側がインスタンスでフォームを初期化することに注意してくださいform = ProfileForm(instance=profile)。フォームの送信は投稿でフォームを初期化しますが、それでもインスタンスにバインドしますform = ProfileForm(request.POST, instance=profile)

于 2012-07-06T18:00:41.707 に答える
0

フォームを見ている場合は、Djangoのフォームフレームワーク、特にモデルのフォームから始めることをお勧めします。

于 2012-07-06T17:51:14.170 に答える