如何修复``列表''对象在Python / Django中没有属性``获取''(AttributeError)

时间:2019-09-27 08:19:28

标签: python django

我想使用表单在数据库中创建新对象,但是我无法运行此视图。我需要在哪里进行一些更改?

我试图删除“ def get”功能,但它只是白屏,之后就是空白页面。

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})

2 个答案:

答案 0 :(得分:1)

RadioSelect的第一个参数应该是attrs,但是您正在传递选择。

form字段将负责将选择传递给小部件,因此最简单的解决方案是将代码更改为widget=forms.RadioSelect

airport = forms.ChoiceField(choices=AIRPORT_CHOICES, widget=forms.RadioSelect)

答案 1 :(得分:-1)

您的def get()函数返回add_order.html页面,这就是为什么当您尝试删除函数时,找不到HTML页面显示任何数据,最终它显示为空白。