0

I have a form (https://localhost:8000/generate/) with a text field and a drop down menu that generates a Django object, and data from that object get displayed on the success page. The success page URL looks something like this:

https://localhost:8000/success/?name=bob&theme=music&obj=some-cool-random-slug

On this success page I have a button that essentially should do a "redo" (do the same form post that the generate page did) and redisplay the results on the same success page. A user could also click another button to go back to the original generate page and fill out that form.

In my Django view, I want to fetch the URL data with something like this:

name = request.GET.get('name', '')
theme = request.GET.get('theme', '')

...and then use Ajax to resubmit these data again to the Django view (as described above).

In principle, how would I do this? As @levi points out, posting the data to the /generate/ view is the way to do this. I want the generate form/view workflow to function as expected (when visiting URL /generate/, eg, the generate form), but then also when button is clicked from the success page.

Ajax

$(".redo").click(function() {

    // fetchQueryString() defined elsewhere
    var name = fetchQueryString['name'];
    var theme = fetchQueryString['theme'];

    $.ajax({
        type: 'post',
        url: '/generate/',
        data: { 
            name: name,
            theme: theme
        },
        success: function(response) {
            console.log("ajax succeeded");
            location.reload();
        },
        error: function(response) {
            console.log("ajax failed");
        },
    });
});

UPDATE

I have this mostly working. I have edited my Ajax script above. Now the only thing that isn't working is after the "redo" action is called, the URL doesn't update with the new data, although when running the form from the beginning results in the correct URL. I'm not sure why the URL isn't updating if the view itself sets the URL with the new data.

How would I do this?

nicorellius
  • 3,715
  • 4
  • 48
  • 79
  • when user clicks on "redo" button, the ajax calls should post the data again to /generate/ ? – levi Feb 06 '15 at 00:28
  • That is what I have the Ajax doing now, right? I'm not sure how to make sure the data from the URL gets passed to the view. – nicorellius Feb 06 '15 at 00:31

1 Answers1

0

Post data in the Ajax script to /generate/.

Ajax

$(".redo").click(function() {

    // fetchQueryString() defined elsewhere
    var name = fetchQueryString['name'];
    var theme = fetchQueryString['theme'];

    $.ajax({
        type: 'post',
        url: '/generate/',
        data: { 
            name: name,
            theme: theme
        },
        success: function(response) {
            console.log("ajax succeeded");
            location.reload();
        },
        error: function(response) {
            console.log("ajax failed");
        },
    });
});

The fetchQueryString() script is a modified version from here:

How can I get query string values in JavaScript?

Community
  • 1
  • 1
nicorellius
  • 3,715
  • 4
  • 48
  • 79