gpt4 book ai didi

javascript - 使用 {% url %} 进行 NoReverseMatch

转载 作者:行者123 更新时间:2023-12-03 06:28:37 25 4
gpt4 key购买 nike

我正在尝试遵循此处给出的示例: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'),

预计到达时间:

我现在已经尝试了以下所有方法,但都不起作用:

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

最佳答案

您缺少网址名称:

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

关于javascript - 使用 {% url %} 进行 NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38531445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com