2

I have HttpResponseRedirect() function which constructs the required url:

my create view:

def create(request):
    entry = Char_field(field=request.POST['record'])
    entry.save()
    return HttpResponseRedirect(reverse('db:index_page',kwargs={'redirected':'true'}))

the view which the HttpResponseRedirect() redirects to is:

def index(request):
    redirected = False
    template= 'app/index.html'
    try:
        if request.POST['redirected'] is 'true':
            redirected = True
    except:
        pass
    return render(request,template,{'redirected':redirected})

However it returns an error:

NoReverseMatch at /app/create/
Reverse for 'index_page' with arguments '()' and keyword arguments '{'redirected': 'true'}' not found.

urls.py:

urlpatterns = patterns('',
    url(r'^$',views.index,name='index_page'),
    url(r'^get_record/$',views.get_record,name='get_record'),
    url(r'^create/$',views.create_html,name='create_path'),
    url(r'^add/$',views.create,name='add_record')
)

Why is this so and Is it possible send POST data to the index_page view through the reverse() function?

K DawG
  • 13,287
  • 9
  • 35
  • 66
  • Show us your `urls.py` with the definition of `db:index_page`. – Matthias Nov 02 '13 at 07:27
  • Pretty clear: `index_name` doesn't take any parameters in the URL definition, so `reverse` can't find the location and throws an error. – Matthias Nov 02 '13 at 07:36
  • @Matthias that's the point, I'm trying to send the data as a `POST` request through the `reverse` function – K DawG Nov 02 '13 at 07:38
  • @KDawG Well, you can't do that, because then you'd need to change the headers. – Games Brainiac Nov 02 '13 at 07:41
  • HTTP redirects can't contain POST data. see http://stackoverflow.com/questions/3024168/django-how-do-i-redirect-a-post-and-pass-on-the-post-data for details and possible solutions – alko Nov 02 '13 at 08:57

1 Answers1

5

This question isn't actually anything to do with POST data. You are simply trying to reverse with a parameter, but the URL for index_page doesn't take one. That would be true whether or not you were wanting to POST to that URL.

Then, of course, you don't actually POST to that URL, you simply return HttpResponseRedirect, which does a 302 redirect.

I'm not sure why you think you need this parameter in the POST. If you don't want to put it in the URL pattern, why not just in the GET params?

url = reverse('db:index_page') + '?redirected=true'
return HttpResponseRedirect(url)

and in the receiving view:

redirect = request.GET.get('redirected')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895