简体   繁体   English

如何修复“列表”object 在 Python/Django 中没有属性“get”(AttributeError)

[英]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.

相关问题 python django:如何修复错误说:AttributeError at / 'Home' object has no attribute 'get' - python django: How to fix error saying: AttributeError at / 'Home' object has no attribute 'get' 如何修复'AttributeError:'NoneType'对象在python中没有属性'get'错误 - How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python 无法修复 Python 错误 AttributeError: 'int' object 没有属性 'get' - Cant fix Python error AttributeError: 'int' object has no attribute 'get' 如何修复 Django 中的“/api/doc 'AutoSchema' 对象没有属性 'get_link' 属性错误”错误 - How to fix " AttributeError at /api/doc 'AutoSchema' object has no attribute 'get_link' " error in Django 如何修复 AttributeError: 'list' 对象没有属性 'encode' - How to fix AttributeError: 'list' object has no attribute 'encode' 如何在加载模型时使用Tensorflow / Keras修复python中的'AttributeError:'list'对象没有属性'shape'错误 - How to fix 'AttributeError: 'list' object has no attribute 'shape'' error in python with Tensorflow / Keras when loading Model 如何修复python中的“AttributeError:type object has no attribute”? - How to fix "AttributeError: type object has no attribute" in python? “如何解决'AttributeError:'NoneType'对象在Python中没有属性'tbody'错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? 如何修复“AttributeError: 'str' object has no attribute 'content'”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error 修复 Python 中的“AttributeError: type object has no attribute” - Fix “AttributeError: type object has no attribute” in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM