[英]Getting a value from request, django
I am trying to get a value from request
and really suprised an error is being raised. 我试图从
request
获取一个值,真的很惊讶正在引发错误。
def product_view(request):
lookup_type_user = request.GET.get('lookup_type', '')
LOOKUP_TYPE_CHOICES = (
('gt', '>'),
('lt', '<'),
)
class ProductFilter(django_filters.FilterSet):
lookup_type = django_filters.ChoiceFilter(choices=LOOKUP_TYPE_CHOICES)
price = django_filters.NumberFilter(lookup_type=lookup_type_user)
This line, being basically the same, works fine. 这条线基本相同,可以正常工作。
price = django_filters.NumberFilter(lookup_type='gte')
I am not posting the error message because it's a package relevant one and since the line above where I provided lookup_type
by hand did not raise anything I am assumming it has nothing to do with that package but the code above. 我没有发布错误消息,因为它是与程序包相关的错误消息,并且由于上面我手动提供
lookup_type
的行没有引发任何事情,我假设它与该程序包无关,但与上面的代码无关。
Can you see what goes wrong here? 您能看到这里出了什么问题吗?
@EDIT @编辑
Is there a way I can print out the request
to see what it contains exactly? 有没有办法我可以打印出该
request
以查看其中包含的内容?
I got it working. 我知道了 It was my ignorance.
这是我的无知。 I had to redefine
lookup_type
in forms.py
instead. 我不得不重新
lookup_type
在forms.py
代替。 Like this: 像这样:
lookup_type = forms.ChoiceField(choices=LOOKUP_TYPE_CHOICES)
and not: 并不是:
lookup_type = django_filters.ChoiceFilter(choices=LOOKUP_TYPE_CHOICES)
Because what django-filter was doing, it was trying to filter the lookup_type
field, which does not exist in my models. 因为django-filter在做什么,所以它试图过滤
lookup_type
字段,该字段在我的模型中不存在。 It was throwing an error FieldError at/. Cannot resolve keyword 'lookup_type' into field
它
FieldError at/. Cannot resolve keyword 'lookup_type' into field
抛出错误FieldError at/. Cannot resolve keyword 'lookup_type' into field
FieldError at/. Cannot resolve keyword 'lookup_type' into field
, which I did not know about because another app I am using - django_tables2 modified this error to something else, which tricked me successfully. FieldError at/. Cannot resolve keyword 'lookup_type' into field
,我不知道这是因为我正在使用的另一个应用程序-django_tables2将此错误修改为其他错误,从而成功地欺骗了我。
It's probably a useless thread now but I just want to let pleople who tried to solve this, know. 现在它可能是一个无用的线程,但是我只想让尝试解决此问题的人知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.