django - Django: forms.ChoiceField, overriding forms __init__
问题描述
I am trying to implement a forms.ChoiceField() with values from a view. I already can do it if I declare the choices in the forms.py, but that's not what I need.
views.py:
def add_crime(request):
values = [('1','um'),('2','dois'),('3','tres')]
if request.method == 'POST':
form = AddCrimeForm(request.POST, values)
if form.is_valid():
# do stuff
return redirect('show_crime')
else:
form = AddCrimeForm(request.GET)
return render(request, 'add_crime.html', {'form': form})
forms.py:
class AddCrimeForm(forms.Form):
tests = forms.ChoiceField()
def __init__(self, testList, args, **kwargs):
self.testList = testList
super(AddCrimeForm,self).__init__(*args, **kwargs) # testList not in args!
self.fields['tests'].widget = forms.CheckboxSelectMultiple()
self.fields['tests'].choices = self.testList
Error:
AttributeError: 'tuple' object has no attribute 'get'
From the feedback, do I have to implement another __init__
with one argument in forms.py? That's what I would try in Java.
My final goal is to implement two ChoiceField and the second one would depend from the first one. Is there a better way?
解决方案
这里有两个错误:第一个是在AddCrimeForm
. *args
在__init__
标题中需要一个星号 ( ) *
:
class AddCrimeForm(forms.Form):
tests = forms.MultipleChoiceField()
def __init__(self, testList, *args, **kwargs):
self.testList = testList
super(AddCrimeForm,self).__init__(*args, **kwargs) # testList not in args!
self.fields['tests'].widget = forms.CheckboxSelectMultiple()
self.fields['tests'].choices = self.testList
如果您想选择多个选项,您可能还想制作forms
一个MultipleChoiceField
[Django-doc]。
在您看来,您应该构造一个AddCrimeForm
, 列表values
作为第一个元素,因为它被定义为您的第一个元素AddCrimeForm
:
def add_crime(request):
values = [('1','um'),('2','dois'),('3','tres')]
if request.method == 'POST':
form = AddCrimeForm(values, request.POST)
if form.is_valid():
# do stuff
return redirect('show_crime')
else:
form = AddCrimeForm(values)
return render(request, 'add_crime.html', {'form': form})
除非您想处理表单中的查询字符串,否则通常不会传递request.GET
给表单,否则它将采用查询字符串中定义的值。
推荐阅读
- java - java中的URL路径匹配器(Spring)
- nlp - Spacy:词向量使用什么算法?
- c# - Unity C# 从同一个 GameObject 上的另一个脚本中获取变量
- spring-boot - 如何确保只有一个消费者实际使用已发布的消息?
- matlab - 在 MATLAB 中对指定轴进行插值
- c# - InstanceContextMode = InstanceContextMode.PerSession 不工作
- http - Ionic http 会话在大约 5 秒后丢失。如何在 ionic http post 上启用 KeepAlive?
- php - 具有分组和跨列比较的 SQL (PHP)
- c++ - 国际象棋c ++控制台游戏,区分白色和黑色
- javascript - 孤立世界中的访问节点和电子模块