3

アクティベーション キー ( /user/activate/123123123 ) を含むアクティベーション URL があります。それは問題なく動作します。get_context_data は、それをテンプレートにうまく組み込むことができます。私がやりたいのは、キーフィールドの初期値としてそれを持っているので、ユーザーは登録時に作成されたユーザー名とパスワードを入力するだけで済みます。

フィールドをテンプレートにハードコーディングせずに、コンテキストまたは get() からキーを取得するにはどうすればよいですか?

class ActivateView(FormView):
    template_name = 'activate.html'
    form_class = ActivationForm
    #initial={'key': '123123123'} <-- this works, but is useless

    success_url = 'profile'

    def get_context_data(self, **kwargs):
        if 'activation_key' in self.kwargs:
            context = super(ActivateView, self).get_context_data(**kwargs)
            context['activation_key'] = self.kwargs['activation_key']
            """
            This is what I would expect to set the value for me. But it doesn't seem to work.
            The above context works fine, but then I would have to manually build the 
            form in the  template which is very unDjango.
            """
            self.initial['key'] = self.kwargs['activation_key']  
            return context
        else:
            return super(ActivateView, self).get_context_data(**kwargs)
4

1 に答える 1

5

オーバーライドget_initialして、動的な初期引数を提供できます。

class ActivationView(FormView):
    # ...

    def get_initial(self):
        return {'key': self.kwargs['activation_key']}
于 2013-12-31T19:02:20.063 に答える