在Django Views中将代码从FBV转换为CBV

时间:2018-05-07 17:45:46

标签: django django-class-based-views

我目前正在将基于函数的视图转换为基于类的视图。在我的模板上,我通过function iterate() { var $list = $(".mini"), // get the whole list of elements i = 0; // i is the index of the currently animated element from list function next() { // the function that when called will get the current element from list (if exists) and starts that element's animation if(i < $list.length) { // if there is still un-animated elements in $list setTimeout(function() { // animate the current element go($list.eq(i), next); // specify that next will be called when the current element's animation is done }, 500); i++; // increment i of course } } next(); // call next to start the magic } function go(element, complete) { // go will take an element to be animated, and a function that will be called when that animation is done element.animate({left: "500px"}, 200, complete); // simply call animate with that additional function (see jQuery#animate docs) } 标记获得了一个值。

input

获取值后,我使用FBV中的以下值进行处理。

<input name="search_text" type="text">

但是现在,我想将代码转换为CBV。 我怎么能这样做?

更新

def search(request):
    search_text = request.GET.get('search_text')

    search_stores = Store.objects.filter(Q(businessName__icontains=search_text) | Q(mKey__icontains=search_text))

1 个答案:

答案 0 :(得分:2)

以下是使用ListView

的解决方案
from django.views.generic import ListView

class SearchView(ListView):
     template_name = 'template.html'
     model = Store

     def get_queryset(self):
         search_text = self.kwargs['search_text']
         object_list = self.model.objects.filter(Q(businessName__icontains=search_text) | Q(mKey__icontains=search_text))
         return object_list