Extraña respuesta ajax

estoy llamando sortMovie() ajax function, whenever the user changes a drop down box. This is working, but I'm getting some strange output. The ajax response loads the same template twice. I think it is because of return render_to_response('movie/movie_list.html',{'movies':movies})

Can anyone help me? Here is captura de pantalla of the issue. Thanks.

The dropdown code:

<select name="movielist" onchange="sortMovie(this.value)">
  <option value="">Choose</option>
  {% for category in categories %}
  <option value="{{ category.id}}">{{category.name}}</option>
  {% endfor %}                    

The code from the view:

def movie_sort(request):
        movies = Movie.objects.filter(language=request.GET.get('q'))
        movies = Movie.objects.all()
    return render_to_response('movie/movie_list.html',{'movies':movies})

And the ajax:

function sortMovie(str)
  if (str=="")
    document.getElementById("txtHint").innerHTML = "";

  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

  xmlhttp.onreadystatechange = function()
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
  xmlhttp.open("GET", "/moviesort/?q=" + str, true);

Editar: Aquí está mybase. Aquí está mi movie_details.htm. Last one is movie_list.html Update : I found If I remove {% extends 'base.html' %} desde movie_list.html'. Then It worked. So It mean I have to create differenttemplate` for this view.

preguntado el 08 de noviembre de 11 a las 17:11

Do you happen to have a brief snippet that shows the html of the page? In the screenshot it looks like you are reloading the entire page into an element, but without the source it is a bit hard to tell. -

@jro Hey buddy, I have got idea from you. problem solved.Do you happen to have a brief snippet that shows the html of the page? I checked my html code. And found I'm extending useless templates. I just removed the useless templates. And its working fine. Thank you very much. -

@jro I want to accept your answer. Is it possible? -

I can formulate an answer, sure. Are you certain the answer Daniel has given already doesn't solve your problem? I'm not sure that is actually a problem, but might be an issue as well. If so, just accept his answer. Otherwise, please post back and I'll add the comment as the answer. -

@jro getting same error. :-( I'm posting code to paste. -

2 Respuestas

Judging by the screenshot you posted and based on your comment, what seems to happen is that the entire page is inserted into the innerHTML of the tag you are updating. The issue then is that either the ajax request reloads the entire page, or the HTML that is received from the ajax call contains more markup than you expect it to contain.

Based on the sources you posted, the latter seems the case: remove the {% extends 'base.html' %} desde movie_list.html, and everything should be fine.

respondido 08 nov., 11:22

Well, yes. Your Ajax is posting to the same view as the original page, and the view is just returning a rendered template which your Javascript is faithfully inserting into the page.

Your view should be checking for an Ajax query and returning JSON, rather than a rendered template. Usually you would use request.is_ajax(), but that flag will not be set as you are not using a JS library (which you really should be, these days). You might find it easier to use a separate view just for your Ajax request, and return JSON from there.

respondido 08 nov., 11:21

Thanks for info. I will take care of these things next time. thanks anyways - Sin libertad

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.