NoReverseMatch使用{%url%}

时间:2016-07-22 16:35:41

标签: javascript python django

我正在尝试按照此处给出的示例:Refresh <div> element generated by a django template

但我最终得到了:

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

我看到很多关于此错误的搜索结果,但它们似乎都不相关,除非我完全遗漏了某些内容。

search.html中的Javascript :(更新)

      <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 :(更新)

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

ETA:

我现在已经尝试了以下所有方法,但都没有效果:

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'),  

1 个答案:

答案 0 :(得分:1)

您错过了网址名称:

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

在你的模板中:

{% 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'),
    #...
]