一个班级的结果如何在另一个班级中使用

时间:2019-11-23 09:52:50

标签: python django

Class SearchResultsView(ListView):     型号=用户     template_name ='all_users / doctor / search.html'

def get_queryset(self):  # new
    query = self.request.GET.get('q')
    object_list = User.objects.filter(Q(username__icontains=query))
    return object_list

class PostCreateView(LoginRequiredMixin, CreateView):
    template_name = 'all_users/doctor/post_form.html'
    model = Post
    fields = ['title', 'content']

    def form_valid(self, form):
        form.instance.author = self.request.object_list
        return super().form_valid(form)

1 个答案:

答案 0 :(得分:0)

尝试在创建视图中添加获取上下文数据并将其传递给模板。

def get_context_data(self, **kwargs):
    query = self.request.GET.get('q')
    context['object_list'] = User.objects.filter(Q(username__icontains=query))
    return context

或者在您的表单模板中添加带有带有搜索网址的action属性的表单标签