在我的最后一个问题中,我在选择小部件中显示导入的选项时遇到了麻烦,因为我遗漏了一段(应该是)明显的代码。现在,我的 GET(似乎)工作得很好。问题是我在 POST 中做错了什么。我无法弄清楚应该如何在 POST 中处理 kwargs。如果我在我的 post 方法中包含 kwargsform = Enter_SW_Room(request.POST, **kwargs)我收到错误__init__() got multiple values for argument 'data'。环顾堆栈溢出,我发现此错误的大部分问题源于在 super 中调用 self (或其他调用错误)。例如这个问题/解决方案。如果我尝试通过不传递 kwargs 来解决此问题,则验证将失败。(我认为是因为现在适当的字段有一个值,但选择消失了。)所以,我认为要么我在做一些简单但又重要的错误,要么我需要修改表单验证。(要么 ???)这是所有代码(为简洁起见剪下):''' views.py '''class DockingBay(FormMixin, TemplateView): form_class = Enter_SW_Room def get_form_kwargs(self): kwargs = super().get_form_kwargs() # snip; create my_avatar_choices, which is more than just a queryset kwargs['avatar_list'] = my_avatars_choices return kwargs def get(self, request, *args, **kwargs): kwargs = self.get_form_kwargs() # snip my_rooms_list = [] # used in the template form = Enter_SW_Room(**kwargs) args = {'form': form, 'my_rooms_list': my_rooms_list} return render(request, self.template_name, args) def post(self, request, **kwargs): print(request.POST) kwargs = self.get_form_kwargs() form = Enter_SW_Room(request.POST, **kwargs) if form.is_valid(): # snip - logic to enter the room with correct passcode return redirect('swdice:swroom', swroom_id) else: # snip - error handling will go here'''forms.py'''class Enter_SW_Room(forms.ModelForm): class Meta: model = EnterSWroom widgets = {'default_avatar': forms.Select()} fields = ('room_number', 'passcode', 'default_avatar') def __init__(self, *args, **kwargs): imported_list = kwargs.pop('avatar_list') super().__init__(*args, **kwargs) self.fields['default_avatar'].choices = imported_list default_avatar = forms.ChoiceField(choices=[])
添加回答
举报
0/150
提交
取消