0
votes

I am new to Django so I will try to describe my scenario the best I can:

  1. The user enter some String to search
  2. The data is returned from the server and the data is populated in the table accordingly
  3. The user can then click on a button and data will appear in modal dialog
  4. In the dialog box the user can change the search value and hit the search button to re search for the new value
  5. a post request is sent to the server and the function is invoked in the views file
  6. In the browser network under dev mode (F12) I can see that the response hold the new data but the page itself shows the old data

Does someone have an idea why this happens and how this can be resolved?

bs modal box

              var url = "{% url "ebanalyzer:search" %}";
          //window.location=url;
          $.post(url,{"query" : newValue,csrfmiddlewaretoken: '{{ csrf_token }}'},function (data,status) {
              //alert("data: " + data + "status: " +  status)

newValue param obtain the search field value

from urls file # /search url(r'^search/$', views.post_new, name='search'),

from views file @ensure_csrf_cookie def post_new(request): print(request.POST['query']) if request.POST["query"] != '': print("Rendering") return render(request, 'ebanalyzer/index.html', {"object_list":ebInfo.getSoldItems(request.POST["query"]) })

So just to emphasize the flow, user click on "paste" button --> ajax post is performed--> post_new is invoked (so far so good) --> response return to the client side but page is not updated with new data

1
Can you show your code, those that you were trying to do ? That'll be helpful if you want helps. - Lemayzeur
@Full.C edited with code an screenshot - user2145673
Ok, you should replace the DOM with you new data from you js file. - Lemayzeur
Can you give me an example what should I do exactly? - user2145673
put this in your $.post ajax request: .done(function(data){ $("#id_in_dom").replaceWith($("#id_in_dom",$(data))); }) - Lemayzeur

1 Answers

0
votes
csrfmiddlewaretoken:'{{csrf_token}}'

Don't put the csrf like that, {{csrf_token}} means the HTML code. Put the csrf_token in your template, wherever in your HTML code.

Try to use your ajax request like that instead of what you did:

$.ajax({
    url:url,
    data:{
        query:newValue,
        csrfmiddlewaretoken:$('input[name="csrfmiddlewaretoken"]').val(),
    },
    success:function(data){
        $("#container").replaceWith($("#container",$(data)));
    }
});

Hope this helps.