0

I'm trying to follow the examples given here: Refresh <div> element generated by a django template

But I end up with:

NoReverseMatch at /search/: 
Reverse for '' with arguments '()' and keyword arguments 
'{u'flights':[{FLIGHTS}]}' not found. 0 pattern(s) tried: []"

I see lots of search results for this error, but none of them seem relevant, unless I'm completely missing something.

Javascript in search.html: (UPDATED)

      <script>
        var flights = {{ flights | safe }}

        $.ajax({
        url: {% url 'search_results' flights %},
        success: function(data) {
        $('#search-results').html(data);
        }
        });
      </script>

views.py:

def search_results(request, flights):
    return render_to_response('search_results.html', flights)

urls.py: (UPDATED)

url(r'^search/search_results/(?P<flights>[^/]*)$', "fsApp.views.search_results", name='search_results'),

ETA:

I've now tried all of the following, and none work:

url(r'^search/(?P<flights>[^/]*)$', "fsApp.views.search_results", name='search_results'),                                                       
url(r'^search/search_results/(?P<flights>[^/]*)$', "fsApp.views.search_results", name='search_results'),
url(r'^search/(?P<flights>[^/]*)/search_results/$', "fsApp.views.search_results", name='search_results'),  
Community
  • 1
  • 1
ballardjw2
  • 27
  • 1
  • 6

1 Answers1

1

You're missing the url's name:

url(r'^search/(?P<flights>[^/]*)/search_results/$', "fsApp.views.search_results", name='search_results'),

In your template:

{% url 'search_results' flights as url_search %}

<script>
  ...
  url: '{{ url_search }}',
  ...
</script>

Example from Django:

from django.conf.urls import url

from . import views

urlpatterns = [
    #...
    url(r'^articles/([0-9]{4})/$', views.year_archive, name='news-year-archive'),
    #...
]
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • You current url definition is not expecting a `flights` param – Gocht Jul 22 '16 at 16:43
  • @ballardjw2 I edited my answer, with an example to ask for a kwarg in your url, check docs linked in my answer to learn more. – Gocht Jul 22 '16 at 16:44
  • The documentation seems to suggest it should just be "r'^search/(?P[^/]*)/$'"? Which also doesn't work. And another commenter reversed the flights and search_results parts and nope. Still getting the same error. – ballardjw2 Jul 22 '16 at 17:02
  • Yes. But "url(r'^search/(\[.*\])$', "fsApp.views.search_results", name='search_results')," works, sort of. – ballardjw2 Jul 22 '16 at 17:14
  • In your ajax: `'{% url 'search_results' flights %}'`. Don't miss `'` – Gocht Jul 22 '16 at 17:15