Python 在/'处获取类型错误;功能';对象是不可编辑的
在我的.views文件中添加了一个新的def函数后,我一直遇到这个错误('function'对象不可编辑),您有什么想法吗 这样做的目的是使用复选框过滤查询集 以下是my views.py函数:Python 在/'处获取类型错误;功能';对象是不可编辑的,python,django,function,filter,django-views,Python,Django,Function,Filter,Django Views,在我的.views文件中添加了一个新的def函数后,我一直遇到这个错误('function'对象不可编辑),您有什么想法吗 这样做的目的是使用复选框过滤查询集 以下是my views.py函数: def FilterView(request): qs = Product.objects.all() ptag = request.GET.get('ptag') if ptag == 'on': qs = qs.filter(ptag='') qs
def FilterView(request):
qs = Product.objects.all()
ptag = request.GET.get('ptag')
if ptag == 'on':
qs = qs.filter(ptag='')
qs = filter(request)
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
在我的URL中:
from search.views import HomeView, FilterView
urlpatterns = [
url(r'^$', HomeView.as_view(), FilterView),
]
非常感谢 您的代码没有意义。您正在将
HomeView
和FilterView
传递到单个url()
,并且正在对请求对象运行内置的filter
函数?这里应该是一个工作示例,减去无法解释的过滤器()
:
url.py
from search.views import HomeView, FilterView
urlpatterns = [
...
url(r'^$', FilterView, name='filter'),
...
]
def FilterView(request):
ptag = request.GET.get('ptag', '')
qs = Product.objects.all() if ptag != 'on' else Product.objects.filter(ptag='')
#qs = filter(request) # What is this even trying to do?
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
views.py
from search.views import HomeView, FilterView
urlpatterns = [
...
url(r'^$', FilterView, name='filter'),
...
]
def FilterView(request):
ptag = request.GET.get('ptag', '')
qs = Product.objects.all() if ptag != 'on' else Product.objects.filter(ptag='')
#qs = filter(request) # What is this even trying to do?
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
你的代码没有意义。您正在将
HomeView
和FilterView
传递到单个url()
,并且正在对请求对象运行内置的filter
函数?这里应该是一个工作示例,减去无法解释的过滤器()
:
url.py
from search.views import HomeView, FilterView
urlpatterns = [
...
url(r'^$', FilterView, name='filter'),
...
]
def FilterView(request):
ptag = request.GET.get('ptag', '')
qs = Product.objects.all() if ptag != 'on' else Product.objects.filter(ptag='')
#qs = filter(request) # What is this even trying to do?
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
views.py
from search.views import HomeView, FilterView
urlpatterns = [
...
url(r'^$', FilterView, name='filter'),
...
]
def FilterView(request):
ptag = request.GET.get('ptag', '')
qs = Product.objects.all() if ptag != 'on' else Product.objects.filter(ptag='')
#qs = filter(request) # What is this even trying to do?
context = {
'queryset': qs
}
return render(request, "partials/search_form.html", context)
您正在传入homeview和filterview。你需要选择一个 在URL.py中 改变 到
您正在传入homeview和filterview。你需要选择一个 在URL.py中 改变 到
请包括整个堆栈跟踪
qs=filter(request)
应该做什么?为什么在urlpatterns
中使用url(…)
?我认为建议使用path(…)
。为什么要将HomeView.as_view()
和FilterView
作为根url的参数传递?感谢您的回答,非常感谢!对不起,伙计们,我对这一点还是新手,但我愿意学习。:)我使用url(…)而不是路径(…),因为我使用的是Django 1.10.5版本,因为我需要使用Haystack,这个urlpattern是默认的。请包括你的整个堆栈跟踪qs=filter(request)
应该做什么?你为什么在urlpatterns
中使用url(…)
?我认为建议使用path(…)
。为什么要将HomeView.as_view()
和FilterView
作为根url的参数传递?感谢您的回答,非常感谢!对不起,伙计们,我对这一点还是新手,但我愿意学习。:)我使用url(…)而不是路径(…),因为我使用的是Django 1.10.5版本,因为我需要使用Haystack,这个url模式是默认的。非常感谢您的回答,我非常感谢!在这样的类中插入这个函数怎么样?我已经试过了,我再也不会出错了!以下是url:url(r'^$),HomeView.as_view()和视图文件中的代码:`class HomeView(TemplateView):template_name=“home.html”def FilterView(request):ptag=request.GET.GET('ptag','')qs=Product.objects.all()如果ptag!=“on”else Product.objects.filter(ptag='')context={'queryset':qs}返回呈现(请求,“partials/search_form.html”,context)`非常感谢您的回答,我真的很感激!在这样的类中插入此函数怎么样?我已经尝试过了,不再出现错误!以下是url:url(r'^$,HomeView.as_view()视图文件中的代码:`class HomeView(TemplateView):template_name=“home.html”def FilterView(request):ptag=request.GET.GET('ptag','')qs=Product.objects.all()如果ptag!=“on”else Product.objects.filter(ptag=”)context={'queryset':qs}返回render(请求,“partials/search_form.html”,context)`