2017-12-28 14 views
0

私は単純にデータを提示するフォームを実装しようとしており、ユーザーに「受け入れる」または「拒否する」の選択肢を提供します。私はget_context_data()メソッドをオーバーライドして表示したいデータを送信しています。テンプレートには<input type="submit">が2つあります。ここでダミーforms.Formなしでフィールドレスフォームを作成するより効率的な方法は何ですか?

は図である。

class FriendResponseView(LoginRequiredMixin, FormView): 
    form_class = FriendResponseForm 
    template_name = 'user_profile/friend_response.html' 
    success_url = '/' 

    def get_context_data(self, **kwargs): 
     context = super(FriendResponseView, self).get_context_data(**kwargs) 
     context['respond_to_user'] = self.kwargs.get('username') 
     responding_profile = Profile.objects.get(
      user__username=self.request.user) 
     requesting_profile = Profile.objects.get(
      user__username=self.kwargs['username']) 

     friend_object = Friend.objects.get(requester=requesting_profile, accepter=responding_profile) 
     context['accepter_asks'] = friend_object.requester_asks 
     return context 

    def form_valid(self, form): 
     super(PairResponseView, self).form_valid(form) 
     if 'accept' in self.request.POST: 
      # do something 
     else: 
      return redirect('/') 

フォームが任意の入力または選択を受け入れていないので、私はこのダミーの形式があります。

class FriendResponseForm(forms.Form): 
    pass 

を、より効率的なDjangoの方法があるに違いありません同じ結果を達成する。私はそれについてどうやって行くのですか?

答えて

1

最も良い方法は、FormViewを使用するのではなく、基本的なTemplateViewです。次に、提出ロジックを実行するようにpostを定義します。

class FriendResponseView(LoginRequiredMixin, TemplateView): 
    template_name = 'user_profile/friend_response.html' 

    def get_context_data(self, **kwargs): 
     ... 

    def post(self, request): 
     if 'accept' in self.request.POST: 
      # do something 
     else: 
      return redirect('/') 
関連する問題