0

Consider the FormView that I'm overriding, below. Upon successful creation of a new League record, how do I reference that record so that I can redirect the user to a more general edit page specific to that League?

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())
Ben
  • 20,038
  • 30
  • 112
  • 189

1 Answers1

1

this is really straight forward, given an url in the league namespace like

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

you should edit the form_valid method in this way:

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)
DRC
  • 4,898
  • 2
  • 21
  • 35