【发布时间】:2017-03-26 04:26:35
【问题描述】:
我正在尝试在我的网站上制作按钮,当单击这些按钮时,会将特定过滤器应用于我的模型数据库。我要排序的模型项目是“Clothes_Item”,我想应用各种过滤器。我只是想让它在一个非常基本的层面上工作,然后我就可以从那里弄清楚,所以假设我想要一个单一的按钮来显示所有性别 =“unisex”的服装项目(其中性别是我的 Clothes_Item 模型的一个字段)。
index.html
<input class="filter-button" type="button" data="unisex" value="unisex" data_url/>
main.js
$('.filter-button').on('click', function(event) {
event.preventDefault();
var element = $(this); //button that was clicked
$.ajax({
url : 'filter_clothes/',
type: 'GET',
data: { filter_category : element.attr("data") },
});
urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'filter_clothes/', views.filter_clothes, name='filter_clothes'),
#more urls...
views.py
def filter_clothes(request):
filter_category = request.GET.get("filter_category")
clothes = Clothes_Item.objects.filter(gender=filter_category)
return HttpResponseRedirect(reverse('index', kwargs={'clothes': clothes}))
def index(request, **kwargs):
clothes = Clothes_Item.objects.all()
if 'clothes' in kwargs:
clothes = kwargs['clohtes']
if request.user.is_authenticated():
favorite_clothes_ids = get_favorite_clothes_ids(request)
return render(request, 'index.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids})
return render(request, 'index.html', {'clothes': clothes})
我收到“NoReverseMatch at /filter_clothes/”错误,我有一段时间无法解决此问题。任何帮助将不胜感激!
编辑:上述问题已解决,但我没有完全解决问题。新的错误如下: 没有找到带有参数 '()' 和关键字参数 '{'clothes': ]>}' 的 'index' 的反向。尝试了 1 种模式:['$']
【问题讨论】:
标签: python ajax django django-models django-views