Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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
Python django表单\uuuuu初始化\uuuuu不包括字段_Python_Django_Validation_Django Forms_Django Admin - Fatal编程技术网

Python django表单\uuuuu初始化\uuuuu不包括字段

Python django表单\uuuuu初始化\uuuuu不包括字段,python,django,validation,django-forms,django-admin,Python,Django,Validation,Django Forms,Django Admin,我已经为我的一个模型的管理页面做了一个自定义操作,其中可以向所有选定的对象授予权限。有一个中间页面,显示将被授予权限的用户的多选择字段 整个行动,从开始到结束,运作良好,除非我遇到了一些与“南方”无关的问题。作为修复,我必须在表单声明中包含\uuuu init\uuu。但是,现在中间流中的用户和选定对象已正确加载,但一旦单击“确定”按钮,表单将不会验证 根据我调试的结果,当我使用if request.POST.get('POST'):时,单击OK后它不会发回帖子。当我使用if request.m

我已经为我的一个模型的管理页面做了一个自定义操作,其中可以向所有选定的对象授予权限。有一个中间页面,显示将被授予权限的用户的多选择字段

整个行动,从开始到结束,运作良好,除非我遇到了一些与“南方”无关的问题。作为修复,我必须在表单声明中包含
\uuuu init\uuu
。但是,现在中间流中的用户和选定对象已正确加载,但一旦单击“确定”按钮,表单将不会验证

根据我调试的结果,当我使用
if request.POST.get('POST'):
时,单击OK后它不会发回帖子。当我使用
if request.method=='POST':
时,它会在显示表单时发送一个POST,但当单击OK时,表单会失败
is\u valid()
测试,因为缺少字段

当我没有使用
\uuuu init\uuuu

from models import ClientInfo
from customauth.models import ZenatixUser


class SelectUserForm(forms.Form):
    _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)

    def __init__(self, initial, *args, **kwargs):
        super(SelectUserForm, self).__init__(*args, **kwargs)
        try:
            clientObj = ClientInfo.objects.all()[:1].get()
            client = clientObj.corp
            client_name = client.shortName
            client_id = client.cID
            userList = ZenatixUser.objects.filter(corp__cID=client_id)
            #user = forms.ModelMultipleChoiceField(userList, label=client_name + ' users ')
            self.fields['user'] = forms.ModelMultipleChoiceField(userList, label=client_name + ' users ')
            self._selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
        except ClientInfo.DoesNotExist:
            raise Exception('Please add a client info object to the client')


def grant_read_permission(modeladmin, request, queryset):
    opts = modeladmin.model._meta
    app_label = opts.app_label

    form = None

    #if request.method == 'POST':
    if request.POST.get('post'):
        print 'POST'
        form = SelectUserForm(request.POST)
        print form.errors

        if form.is_valid():
            print 'Valid'
            users = form.cleaned_data['user']
            stream_count = queryset.count()
            user_count = len(users)
            for stream in queryset:
                for user in users:
                    print user, stream
                    assign_perm('read_stream', user, stream)

            plural = ['', '']
            if user_count != 1:
                plural[0] = 's'
            if stream_count != 1:
                plural[1] = 's'

            modeladmin.message_user(request, "Successfully granted read permission to %d user%s on %d stream%s." % (
                user_count, plural[0], stream_count, plural[1]))

            return None

    if not form:
        form = SelectUserForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})

    if len(queryset) == 1:
        objects_name = force_unicode(opts.verbose_name)
    else:
        objects_name = force_unicode(opts.verbose_name_plural)

    stream_list = []
    for stream in queryset:
        stream_list.append(stream.path)

    title = _("Are you sure?")

    context = {
        "title": title,
        "objects_name": objects_name,
        'queryset': stream_list,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'tag_form': form,
    }

    return render_to_response("admin/grant_read_permission.html", context,
                              context_instance=template.RequestContext(request))

您已经从args/kwargs列表中提取了一个参数,您称之为initial,并且没有将其传递给super调用。实际上,第一个位置参数是表单数据,没有它表单将永远无效。从方法定义中删除该名称。

现在就可以使用了,非常感谢!)但是可以解释
initial
变量用于设置
\u selected\u action
的值的准确程度?我还删除了
self.\u selected\u action=forms.CharField(widget=forms.MultipleHiddenInput)
行,它仍然有效。困惑的o、 o