Django: Redirect to same page after POST method using class based views
<p>I'm making a Django app that keeps track of tv show episodes. This is for a page on a certain Show instance. When a user clicks to add/subtract a season, I want the page to redirect them to the same detail view, right now I have it on the index that shows the list of all Show instances.</p>

<p><strong>show-detail.html</strong></p>

<pre><code>&lt;form action="{% url 'show:addseason' show=show %}" method="post"&gt;
    {% csrf_token %}

    &lt;button class="btn btn-default" type="submit"&gt;+&lt;/button&gt;
&lt;/form&gt; 

&lt;form action="{% url 'show:subtractseason' show=show %}" method="post"&gt;
    {% csrf_token %}

    &lt;button class="btn btn-default" type="submit"&gt;-&lt;/button&gt;
&lt;/form&gt;
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>class ShowDetail(DetailView):
  model = Show
  slug_field = "title"
  slug_url_kwarg = "show"
  template_name = 'show/show-detail.html'

class AddSeason(UpdateView):
  model = Show
  slug_field = 'title'
  slug_url_kwarg = 'show'
  fields = []

  def form_valid(self, form):
      instance = form.save(commit=False)
      instance.season += 1
      instance.save()

      return redirect('show:index')

class SubtractSeason(UpdateView):
  model = Show
  slug_field = 'title'
  slug_url_kwarg = 'show'
  fields = []

def form_valid(self, form):
    instance = form.save(commit=False)
    if (instance.season &gt;= 0):
        instance.season -= 1
    else:
        instance.season = 0

    instance.save()

    return redirect('show:index')
</code></pre>

<p><strong>urls.py</strong></p>

<pre><code>url(r'^$', views.IndexView.as_view(), name='index'),

url(r'^about/$', views.AboutView.as_view(), name='about'),

# form to add show
url(r'^add/$', views.ShowCreate.as_view(), name='show-add'),

# edit show
#url(r'^(?P&lt;show&gt;[\w ]+)/edit/$', views.ShowUpdate.as_view(), name='show-update'),

# delete show
url(r'^(?P&lt;show&gt;[\w ]+)/delete/$', views.ShowDelete.as_view(), name='show-delete'),

# signup
url(r'^register/$', views.UserFormView.as_view(), name='register'),

# login
url(r'^login/$', views.LoginView.as_view(), name='login'),

# logout
url(r'^logout/$', views.LogoutView.as_view(), name='logout'),

url(r'^error/$', views.ErrorView.as_view(), name='error'),

url(r'^(?P&lt;show&gt;[\w ]+)/$', views.ShowDetail.as_view(), name='show-detail'),

url(r'^(?P&lt;show&gt;[\w ]+)/addseason/$', views.AddSeason.as_view(), name='addseason'),

url(r'^(?P&lt;show&gt;[\w ]+)/subtractseason/$', views.SubtractSeason.as_view(), name='subtractseason'),

url(r'^(?P&lt;show&gt;[\w ]+)/addepisode/$', views.AddEpisode.as_view(), name='addepisode'),

url(r'^(?P&lt;show&gt;[\w ]+)/subtractepisode/$', views.SubtractEpisode.as_view(), name='subtractepisode'),
</code></pre>

<p>I get an error when I try</p>

<pre><code>return redirect('show:detail')
</code></pre>

<p>This is the error</p>

<pre><code>NoReverseMatch at /Daredevil/addseason/
Reverse for 'detail' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
</code></pre>

<p>I am guessing you need to provide a kwarg to identify the show when you redirect, although I can't see the code for your <code>DetailView</code> I would assume the kwarg is called either <code>pk</code> or possibly <code>show</code> going from the convention you've used in <code>AddSeason</code> and <code>SubtractSeason</code>. Try:</p>

<pre><code>redirect('show:detail', kwargs={'show': instance.pk})
</code></pre>

<p><strong>EDIT:</strong> the name of the detail url is <code>'show-detail'</code>, so the scoped viewname would be <code>'show:show-detail'</code> (if it is in the <code>show</code> namespace like the other urls). I still think it would need a kwarg though, try:</p>

<pre><code>redirect('show:show-detail', kwargs={'show': instance.pk})
</code></pre>

python django