Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to refresh in a div values from a model with jquery .ajax() to django
    primarykey
    data
    text
    <p>I have a view which returns a chained object of 3 models</p> <pre><code>def test(request): output=itertools.chain( model1.objects.all(), model2.objects.all(), model3.objects.all() ) return render_to_response('test.html', {'output':output}) </code></pre> <p>In the html, I added an anchor and a jQuery script, which should replace the #output with new values just from model1</p> <pre><code>&lt;html&gt; &lt;head&gt;...&lt;/head&gt; &lt;body&gt; &lt;script&gt; $(document).ready(function() { $("#switch").click(function() { $.ajax({ url: $(this).attr("href"), success: function(result) { //whatever I put here is not triggered } }); }); }); &lt;/script&gt; &lt;a id="switch" href="?model=1"&gt;switch to model 1&lt;/a&gt; &lt;div id="output"&gt; {% for item in output %} &lt;div id="{{item}}"&gt; {{item}} &lt;/div&gt; {% endfor %} &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I tried to put the div#output into a separate template output.html and modified the views.py like so:</p> <pre><code>def test(request, template='test.html'): if request.GET.get('model'): output=model1.objects.all() else: output=itertools.chain( model1.objects.all(), model2.objects.all(), model3.objects.all() ) if request.is_ajax(): template='output.html' return render_to_response(template, {'output':output}) </code></pre> <p>But everytime I click the link, the whole page is refreshed (with the new values from model1). </p> <p>Opera returns just the output.html</p> <p>Been struggling with this for more than 3 days, Im new into Ajax and this is getting very confusing for me.</p> <p>I hope someone can shed some light!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload