2014-12-22 72 views
0

請考慮我在下面覆蓋的FormView。成功創建新的聯盟記錄後,我如何參考該記錄以便我可以將用戶重定向到該聯盟特定的更一般的編輯頁面?如何使用FormView引用新創建的對象

class LeagueView(FormView): 
    template_name = 'leagueapp/addleague.html' 
    form_class = LeagueForm 

    def form_valid(): 
     newleague = ??? #newly created league 
     success_url = '/league/editleague/' + str(newleague.id) 
     return HttpResponseRedirect(self.get_success_url()) 

回答

1

這真的是直線前進,鑑於命名空間中的URL像

url(r'^league/editleague/(?P<id>\d+>)$', LeagueView.as_view(), name='edit') 

你應該以這種方式編輯form_valid方法:

from django.shortcuts import redirect 

class LeagueView(FormView): 
    template_name = 'leagueapp/addleague.html' 
    form_class = LeagueForm 

    def form_valid(self, form): 
     newleague = form.save() 
     return redirect('league:edit', id=newleague.id)