jquery - Refresh only the table content in a Template with new items (Django) -
i implementing table filters. none of existing libraries (such datatables) work me because based on client pagination , cannot bring data db , paginate in client side since has more 5 million items.
so, thing want able write in input field , filter items in table accordingly. url starts is:
this url has html cointained in custom_table.html (see file below), includes sub-template called table_rows.html, the 1 want refresh
to have done following:
structure of project:
project |-app |-static | |-javascript | |-myjs.js | |-templates | |-templates1 | |-custom_table.html | |-table_rows.html | |-views | |-__init__.py #gathers views othe files) | |-ajaxcalls.py | |-modelviews.py | |-urls.py
urls.py
url(r'^table_rows/$', views.tablerows, name='tablerows'),
custom_table.html
#extend , loads here {% block content %} <table id="mytable""> ... <thead> #headers , filters each column </thead> <tbody id="table_body"> {% include "templates1/table_rows.html" %} </tbody> </table> {% endblock %}
inside of table table, there input in every column. can write in them , press enter trigger function call following ajax function:
myjs.js
function getfiltereddata(){ modelname = #get modelname var filters = #get filters $.ajaxsetup({ headers: { "x-csrftoken": getcookie("csrftoken") } }); $.ajax({ url : "../get_filtered_data/", type : "post", data : { modelname: modelname, filters: json.stringify(filters) }, datatype: 'json', success : function(json) { $("#table_body").html('').load("app/views/tablerows.html", {reg_list: json.result}); }, error : function(xhr,errmsg,err) { alert('something went wrong'); console.log(xhr.status + ": " + xhr.responsetext); } }); }
ajaxcalls.py
def get_filtered_data(request): if request.method == "post": try: [...] reg_list = query response filtered data db [...] return jsonresponse({"status": "ok", "result":reg_list}) except exception e: return jsonresponse({"status": "none"}) else: return jsonresponse({"status": "none"})
modelviews.py
def tablerows(request): print("i'm in") return render(request, 'templates1/table_rows.html', { })
everything works fine until point when have load reg_list inside table. following error appears in chrome console:
jquery-3.1.1.min.js:4 post http://127.0.0.1:8000/es/view-containing-the-table/app/views/tablerows.html 404 (not found)
so, aparently, url messed up, since django writing new view in addition 1 existing. maybe there's wrong routing? don't know how proceed, please.
the 404 not found because
$("#table_body").html('').load( "app/views/tablerows.html", {reg_list: json.result} );
needs accessible static asset, not view template. copy/move tablerows.html
app/static/
, update myjs.js
correct static url.
or define view in urls.py
matches app/views/tablerows.html
. looks though part of urls.py
shared i'm missing part of picture.
Comments
Post a Comment