0

I'm following the suggestion at: Refresh <div> element generated by a django template

I'm passing along a few variables, a la:

url: '{% url 'search_results' 'sched_dep_local' flights|escapejs %}',

The problem is that 'flights' is a list of dicts that the search_results template needs access to, and it's pretty large and contains things like apostrophes

[{'foo': 'bar'}, {'foo': 'baz'}] and so on

So the only way I can use it with {% url %} appears to be with escapejs to get rid of the apostrophes, but then in views.py, I need it to be a list of dicts again, so I can do things like:

def search_results(request, sort_key, flights):
    flights = search_utils.sort(flights, sort_key)
    return render_to_response('search_results.html', { 'flights' : flights} )                                                                                                                                    

Is there a simple way to do this? Alternatively, am I going about this whole thing all wrong?

ETA: See also (explains what I'm trying to do and why):

<script>
$(".sort").on("click", function() {
    $.ajax({
        url: '{% url 'search_results' 'sched_dep_local' flights|escapejs %}',
        success: function(data) {
            $('#search-results').html(data);
        }
    });
});
</script>

I have a template (in search_results.html) that prints some data for each flight in flights. I want to sort that data and rerender the template, but I can't figure out how.

Community
  • 1
  • 1
ballardjw2
  • 27
  • 1
  • 6
  • 1
    Why do you need to pass a list of dicts as a variable to the url? Whatever you're trying to achieve, I'm sure there is a better way. – knbk Jul 22 '16 at 18:57
  • Post updated with details. Can you recommend a better way? – ballardjw2 Jul 22 '16 at 19:09

1 Answers1

0

This isn't the right way to deal with complex data. Rather than sending it via the URL, you should be using a POST and sending it in the body of the request: since you're using jQuery, you can just do method: "POST" in that call. In the backend, you can deserialize it from JSON.

However, it does seem a bit strange to do this at all; the data is evidently coming from the Django backend already, so it's not clear why you want to post it back there.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I want to be able to click a button and sort the data without refreshing the page. I can't figure out any better way to do this. – ballardjw2 Jul 22 '16 at 19:04
  • Why do you need to send it to Django in order to sort it? Javascript is perfectly capable of sorting data. – Daniel Roseman Jul 22 '16 at 19:05
  • The issue isn't the sorting. It's the refreshing the template. I need to re-render the template that uses the sorted data. – ballardjw2 Jul 22 '16 at 19:06
  • Why do you need to refresh the template though? You have all the HTML elements already rendered; you just need to move them around. jQuery has all the relevant methods already. – Daniel Roseman Jul 22 '16 at 19:07
  • Can you elaborate? I'm not sure what you mean. – ballardjw2 Jul 22 '16 at 19:08
  • You should ask a new question specifically about ordering a table of elements in jQuery. – Daniel Roseman Jul 22 '16 at 19:10
  • Ok, I'm asking a new question (although I have to wait b/c lol, one post every 90 minutes.) But I think you entirely don't understand what I'm trying to do? – ballardjw2 Jul 22 '16 at 19:18
  • @ballardjw2 No offence but you need to learn some web development and django basics before jumping into coding. Your approach doesn't even make sense, to have a list of parameters in one `{% url %}` so you could just click away without refreshing the page? – Shang Wang Jul 22 '16 at 20:23