【问题标题】:Using Ajax Button to Filter Django Models使用 Ajax 按钮过滤 Django 模型
【发布时间】: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


    【解决方案1】:

    url : 'filter_clothes/' 更改为url : '/filter_clothes/'

    【讨论】:

    • 它仍然给我一个错误:Reverse for 'index' with arguments '()' and keyword arguments '{'clothes': ]>}' not found .尝试了 1 种模式:['$']
    • 看来 changer 已经解决了你原来的错误。这应该可以解决您的新错误。 stackoverflow.com/questions/20993598/…
    • 如果我没记错的话,这个错误是由于用户没有指定名称的值。我还没有在这条线上做过吗? url(r'^$', views.index, name='index'), @PatrickFalvey
    【解决方案2】:

    您需要在 urls.py 中更改索引视图的正则表达式模式,因为它只匹配空字符串,但您将 clothes 参数传递给它。例如,如果clothes 由字母数字字符组成,您可以将模式更改为r'^/(?P&lt;clothes&gt;\w+)/$'

    编辑: 代替 filter_clothes 视图,您可以将获取参数直接传递给索引视图。所以它可能看起来像这样:

    def index(request):
        clothes = Clothes_Item.objects.all()
        filter_category = request.GET.get('filter_category')
        if filter_category:
            clothes = clothes.filter(gender=filter_category)
        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})
    

    【讨论】:

    • clothes 是 Clothes_Item 对象的查询集,因此特定解决方案在这种情况下不起作用。
    • 您是否尝试在 url 标签中传递查询集,例如`{% url 'appname:index' 查询集 %}?
    • 不,我正在使用 ajax 向视图函数发送数据。
    • 出错的原因是filter_clothes 视图中的reverse 函数。您只能将字符串或整数作为参数传递给视图,因为参数是 url 的一部分。我已经为答案添加了建议的解决方案。
    • 我的意思是可能的解决方案。
    猜你喜欢
    • 2012-07-15
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多