[英]How to fix 'list' object has no attribute 'get' (AttributeError) in Python/Django
I want to use form to create new objects in database but i can't run this view.我想使用表单在数据库中创建新对象,但我无法运行此视图。 Where do i need to make some changes?
我需要在哪里进行一些更改?
I was trying to delete "def get" function but it was only white screen, like blank page after that.我试图删除“def get” function 但它只是白屏,之后就像空白页一样。
class AddOrderForm(forms.Form):
airport = forms.ChoiceField(choices=AIRPORT_CHOICES, widget=forms.RadioSelect(AIRPORT_CHOICES))
direction = forms.ChoiceField(choices=DIRECTION_CHOICES)
adress = forms.CharField(widget=forms.TextInput(attrs={"placeholder": "Turmstraße 57"}))
client = forms.CharField()
telephone = forms.CharField(widget=forms.TextInput(attrs={"placeholder": "+49XXXXXXXXX"}))
flight_number = forms.CharField(widget=forms.TextInput(attrs={"placeholder": "LL 0000"}))
plane = forms.DateTimeField(input_formats=['%Y-%m-%d'])
pick_up = forms.DateTimeField(input_formats=['%Y-%m-%d'])
gate = forms.CharField(widget=forms.TextInput(attrs={"placeholder": "G or A11"}))
driver = forms.ChoiceField(choices=DRIVER_CHOICES)
class AddOrderView(View):
def get(self, request):
form = AddOrderForm()
return render(request, 'orders/add_order.html', {'form': form})
def post(self, request, *args, **kwargs):
form = AddOrderForm(request.POST)
if form.is_valid():
order = Order.objects.create(airport=form.cleaned_data['airport'],
direction=form.cleaned_data['direction'],
adress=form.cleaned_data['adress'],
client=form.cleaned_data['client'],
telephone=form.cleaned_data['telephone'],
flight_number=form.cleaned_data['flight_number'],
plane=form.cleaned_data['plane'],
pick_up=form.cleaned_data['pick_up'],
gate=form.cleaned_data['gate'],
driver=form.cleaned_data['driver'])
return redirect(f'order/{order.id}')
return render(request, 'orders/add_order.html', {'form': form})
The first argument to RadioSelect
should be attrs
, but you are passing choices. RadioSelect
的第一个参数应该是attrs
,但您正在传递选择。
The form field will take care of passing choices to the widget, so the easiest solution is to change your code to widget=forms.RadioSelect
.表单字段将负责将选择传递给小部件,因此最简单的解决方案是将代码更改为
widget=forms.RadioSelect
。
airport = forms.ChoiceField(choices=AIRPORT_CHOICES, widget=forms.RadioSelect)
Your def get() function return add_order.html page that's why when you are trying to delete function then Html page is not found to show any data ultimately it shows blank. Your def get() function return add_order.html page that's why when you are trying to delete function then Html page is not found to show any data ultimately it shows blank.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.