Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在django中动态地将参数传递给表单?_Django_Python 3.x_Django Forms - Fatal编程技术网

如何在django中动态地将参数传递给表单?

如何在django中动态地将参数传递给表单?,django,python-3.x,django-forms,Django,Python 3.x,Django Forms,我有一个表单“BasicSearch”,有两个字段。一个是名为“search_by”的选择字段,另一个是名为“search_for”的文本字段。我有客户、供应商、物品、项目等的模型。 我想做的是,通过在文本字段中提供查询,并从选择字段中选择要搜索的内容(模型中的列标题),让用户能够在各自页面上对各种模型执行搜索 我已经在stackoverflow上尝试了几种解决方案,但没有一种对我有效。当我手动创建列标题的字典并将其传递给choice字段时,它可以正常工作 当前搜索表单类如下所示(不起作用) g

我有一个表单“BasicSearch”,有两个字段。一个是名为“search_by”的选择字段,另一个是名为“search_for”的文本字段。我有客户、供应商、物品、项目等的模型。 我想做的是,通过在文本字段中提供查询,并从选择字段中选择要搜索的内容(模型中的列标题),让用户能够在各自页面上对各种模型执行搜索

我已经在stackoverflow上尝试了几种解决方案,但没有一种对我有效。当我手动创建列标题的字典并将其传递给choice字段时,它可以正常工作

当前搜索表单类如下所示(不起作用)

get_col_heads函数:

def get_col_heads(cu):
    all_fields = cu._meta.fields
    all_field_list = []
    for fields in all_fields:
        column_head = (str(fields)).split(".")
        all_field_list.append(column_head[-1])
        field_list = all_field_list[1:-2]
        field_dct = tuple(zip(field_list,field_list))
    return field_dct
客户在view.py中查看类

class IndexView(TemplateView):
    template_name = 'crudbasic/index.html'
    def get_context_data(self,**kwargs):
        context = super().get_context_data(**kwargs)
        context ['page_title'] = ''
        return context

class CustomerView(ListView):
    template_name = 'crudbasic/customers.html'
    model = Customers
    context_object_name = 'customer_data'

    def get_context_data(self,**kwargs):
        context = super().get_context_data(**kwargs)
        context['search_form'] = BasicSearch('customer')
        return context

    def post(self, request, *args, **kwargs):
        search_form = BasicSearch(request.POST)
        if search_form.is_valid():
            data = request.POST.copy()
            qby = data.get('search_by')
            qstrting = data.get('search_for')
            queryparam = qby+'__'+'contains'
            search_list = Customers.objects.filter(**{queryparam:qstrting})
            customer_data = search_list
        return render(request, self.template_name, {'customer_data': customer_data,'search_form':search_form})
当我在表单字段中放置一个init时,为了获取选择相应模型/表的参数,然后从表列标题生成字典,一切都变得混乱了。目前与上面的代码,它是给我以下错误

AttributeError at /customers/
'str' object has no attribute 'get'
如果有人知道怎么做,请帮忙


感谢您

表单类的
基本搜索
方法的自定义
\uuuu init\uuuu
方法不尊重表单可以具有的所有其他参数。你不能这样做。更合适的方法是使用自定义
kwarg
参数,如下所示:

class BasicSearch(forms.Form):
    def __init__(self, *args, **kwargs):
        caller = kwargs.pop('caller')
        super(BasicSearch, self).__init__(*args, **kwargs)
        # ...


# usage
form = BasicSearch(caller='customer')

# usage with POST
form = BasicSearch(request.POST,  caller='customer')

在python中,所有东西都是一个对象,即使是类,因此不需要作为调用者提供一个
“string”
,只需传递类本身:
form=BasicSearch(caller=Customers)

谢谢,它工作起来很有魅力,因此这意味着您无法将一个简单的参数传递给init函数。在这种情况下,你建议直接传递给客户似乎更合适。我会用的,再次谢谢。从昨晚起我就一直在想这个问题,不知道出了什么问题。
class BasicSearch(forms.Form):
    def __init__(self, *args, **kwargs):
        caller = kwargs.pop('caller')
        super(BasicSearch, self).__init__(*args, **kwargs)
        # ...


# usage
form = BasicSearch(caller='customer')

# usage with POST
form = BasicSearch(request.POST,  caller='customer')