2016-12-11 78 views
0

使用CreateView,我試圖保存並返回一個基於表單中選擇的項目的url。這是針對鍛鍊日誌站點的,您根據第一種形式輸入的信息,根據您想要的鍛鍊類型指定第二種形式。我想這樣做沒有JavaScript:Django CBV根據表單數據返回不同的頁面

class WorkoutLogCreateView(CreateView): 
    model = Workout_Log 
    template_name = 'workout/workout_log_create.html' 
    fields = ['date_time', 'modality', 'workout_type', 'workout_model'] 

    #something like this: 
    def get_success_url(self): 
     if workout_type = '5 rounds' 
      return reverse('five-rounds-log-create') 
     elif workout_type = 'Drop Reps' 
      return reverse('drop-reps-log-create') 
     else 
      return reverse('workout-log-menu') 

    def get_context_data(self, **kwargs): 
     context = super(WorkoutLogCreateView, self).get_context_data(**kwargs) 
     context['action'] = reverse('workout-log-create') 
     return context 

回答

0

get_success_url方法,您可以用self.object訪問對象。

def get_success_url(self): 
    if self.object.workout_type = '5 rounds' 
     return reverse('five-rounds-log-create') 
    elif self.object.workout_type = 'Drop Reps' 
     return reverse('drop-reps-log-create') 
    else 
     return reverse('workout-log-menu') 
相關問題