Nathan
Nathan

Reputation: 4925

How to display data resulting from POST in django view?

I have a form that takes a path as input, analyzes files and subfolders in that directory, stores information in a database, then displays some statistics to the user about the files just parsed. This is done currently using a Django view and render_to_response.

According to everything I have read, it's incorrect to use anything other that HttpResponseRedirect when dealing with a POST, as a page generated with POST data would resubmit the form if the page were refreshed.

My issue here is that there's a large amount of summary data ultimately displayed as a result of analyzing files on the provided path. How can I display that data with an httpResponseRedirect? Sending it as GET parameters using django.core.urlresolvers.reverse seems infeasible due to the amount of data.

Upvotes: 0

Views: 1258

Answers (3)

Don
Don

Reputation: 17606

A rough but simple solution is to write your data in a json text file and then read it in your redirect page (this will also save you from rebuilding data on refreshing the page)

Upvotes: 0

Tomasz Zieliński
Tomasz Zieliński

Reputation: 16356

I assume that your POST handle creates some databse object out of a submitted form data, is this correct? If so, then you can do something like this (:

    my_obj = MyModel.objects.create(**form.cleaned_data) # This is the part yuo already have, most probably expressed with some other code, but still..
    return HttpResponseRedirect('/url/to/redirect/?id=%d' % obj.id)

The redirect like should in fact use reverse() function, and I think that you should have an URL for editing MyModel objects. Then you could do:

    return HttpResponseRedirect(reverse('edit-mymodel', (), {'id': obj.id}))

The relevant URL would look like:

url('^edit/mymodel/(?P<id>\d+)$', 'apps.myapp', name='edit-mymodel')

Upvotes: 0

Related Questions