Django redirect to previous page not working
<p>Hi I want to redirect to the previous after submitting the form in django. It is successfully redirecting to the previous page but the post action is not happening. I am able to successfully post the data if I remove the action attribute.</p>

<p>This is my form template.</p>

<pre><code>{% extends 'musicapp/base.html' %} {% block content %}
&lt;!-- &lt;h1&gt;New post&lt;/h1&gt; --&gt;
&lt;form method="POST" action={{ request.META.HTTP_REFERER }} class="post-form"&gt;{% csrf_token %}



&lt;div class="add_track_form" &gt;

  &lt;div class="col-sm-6"&gt;
      &lt;section class="panel panel-default"&gt;
          &lt;header class="panel-heading font-bold"&gt;Add Genre&lt;/header&gt;
          &lt;div class="panel-body"&gt;
              &lt;form action="{% url 'post_list' %}" role="form"&gt;
                  &lt;div class="form-group"&gt;

                      &lt;label&gt;Title&lt;/label&gt;
                       {{ form.name }}
                  &lt;/div&gt;
                  &lt;input type="hidden" name="next" value="{{ request.path }}"&gt;
                  &lt;button type="submit" class="btn btn-sm btn-default"&gt;Submit&lt;/button&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/section&gt;
  &lt;/div&gt;

&lt;/div&gt;


&lt;/form&gt;

{% endblock %}
</code></pre>

<p>This is the views.py file</p>

<pre><code>from django.shortcuts import render, get_object_or_404, redirect, render_to_response
from django.http import HttpResponseRedirect
from .forms import AddTrackForm, AddGenreForm
from django.http import HttpResponse
from .models import Track, Genre
from . import getAlbumArt

# Create your views here.
def home(request):
    tracks = Track.objects.all().order_by('created_date')
    return render(request, 'musicapp/post_list.html', {'tracks': tracks})

def post_detail(request, pk):
    track = get_object_or_404(Track, pk=pk)
    # print post
    return render(request, 'musicapp/post_detail.html', {'track': track})
# def post_new(request):
#     form = AddTrackForm()
#     return render(request, 'musicapp/post_edit.html', {'form': form})

def post_new(request):
    if request.method == "POST":
        form = AddTrackForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            artist = request.POST.get("artist", "")
            album = request.POST.get("album", "")
            # print artist, album
            urlArt = getAlbumArt.getAlbumArtURL(album, artist)
            print urlArt
            if urlArt:
                post.cover_image_url = urlArt
            if post.album == '*':
                post.album = 'Unknown'
            post.save()
            tracks = Track.objects.all().order_by('created_date')
            return render(request, 'musicapp/post_list.html', {'tracks': tracks})
    else:
        form = AddTrackForm()
    return render(request, 'musicapp/post_edit.html', {'form': form})

def post_edit(request, pk):
    post = get_object_or_404(Track, pk=pk)
    if request.method == "POST":
        form = AddTrackForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            artist = request.POST.get("artist", "")
            album = request.POST.get("album", "")
            # print artist, album
            urlArt = getAlbumArt.getAlbumArtURL(album, artist)
            print urlArt
            if urlArt:
                post.cover_image_url = urlArt
            if post.album == '*':
                post.album = 'Unknown'
            post.save()
            tracks = Track.objects.all().order_by('created_date')
            return redirect('post_detail', pk=post.pk)
    else:
        form = AddTrackForm(instance=post)
    return render(request, 'musicapp/post_edit.html', {'form': form})

def genre(request):
    tracks = Track.objects.all().order_by('created_date')
    genres = Genre.objects.all()
    return render(request, 'musicapp/genre.html', {'genres': genres, 'tracks': tracks})

def genre_songs(request, pk):
    genres = Genre.objects.all()
    genre_obj = get_object_or_404(Genre, pk=pk)
    tracks = genre_obj.track_set.all()
    # print post
    return render(request, 'musicapp/genre_songs.html', {'genres': genres, 'genre' : genre_obj, 'tracks': tracks})

def genre_new(request):
    if request.method == "POST":
        form = AddGenreForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            genres = Genre.objects.all()
            print request.META.HTTP_REFERER
            next = request.POST.get('next', '/')
            # return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
            # print next
            # return HttpResponseRedirect(next)
            # return HttpResponseRedirect('/')
            return render_to_response('musicapp/post_edit.html', {'form': AddTrackForm()})
    else:
        form = AddGenreForm()
    return render(request, 'musicapp/genre_edit.html', {'form': form})
</code></pre>

<p>And these are the URL routings.</p>

<pre><code>from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='post_list'),
    url(r'^track/(?P&lt;pk&gt;\d+)/$', views.post_detail, name='post_detail'),
    url(r'^track/new/$', views.post_new, name='post_new'),
    url(r'^track/(?P&lt;pk&gt;\d+)/edit/$', views.post_edit, name='post_edit'),
    url(r'^genre/$', views.genre, name='genre_list'),
    url(r'^genre/(?P&lt;pk&gt;\d+)/$', views.genre_songs, name='genre_songs'),
    url(r'^genre/new/$', views.genre_new, name='genre_new'),
]
</code></pre>