Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango ajax collection change
    text
    copied!<p>I have list of articles on the page in table and i want to sort it with ajax by clicking on the button.</p> <p>I use dajaxice, it sends request to ajax.py to <b>example1</b> method and gets responce JSON data in <b>my_js_callback</b> </p> <p>HTML: </p> <pre><code>&lt;script&gt; function my_js_callback(data) { alert(data) $("#article_view_tbody").html("") for (i=0;i&lt;data.length;i++) article=data[i] $("#article_view_tbody").append("&lt;tr&gt;&lt;td&gt;" + data[i].title...) // here i need to set url for article edit } &lt;/script&gt; &lt;button type="button" onclick="Dajaxice.content.example1(my_js_callback);"&gt;Click Me!&lt;/button&gt; &lt;table&gt; &lt;tbody id="article_view_tbody"&gt; {% for article in articles %} &lt;tr&gt; &lt;a class="article_link" href="{% url article_detail article.id %}"&gt; {{ article.title }} &lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; ... </code></pre> <p>ajax.py:</p> <pre><code>from django.utils import simplejson from django.core import serializers from dajaxice.decorators import dajaxice_register from models import Article def get_by_date_queryset(): return Article.objects.all().order_by('created_on') @dajaxice_register def example1(request): """ Handling sorting requests """ queryset = get_by_date_queryset() json_serializer = serializers.get_serializer("json")() return json_serializer.serialize(queryset, ensure_ascii=False) </code></pre> <p>I can't pass {% url %} tags in the JS. SO is it a way to reload tbody with ajax without js data appending?</p>
 

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