gpt4 book ai didi

Django 形成干净的数据

转载 作者:行者123 更新时间:2023-12-04 22:57:54 27 4
gpt4 key购买 nike

我试图在表单中使用 clean 方法来清理数据,但是当我这样做时,我没有在表单中看到验证错误,它是 django 中唯一的错误页面。

我有编辑和创建表单,我想在一个地方进行清理,但不要复制到 2 个 View 。

你能给我一些建议吗?
我阅读了有关表格中干净和有效的文档,但我仍然有问题。我举个例子:

Views.py

@user_passes_test(lambda u: u.is_staff, login_url='/account/login/')
def client_create(request):

dict = {}

if request.method == 'POST':
form_user = ClientUserCreateForm(request.POST, prefix="user")
form_client = ClientForm(request.POST, prefix="client")
if form_user.is_valid() and form_client.is_valid():
obj_user = form_user.save(commit=False)
obj_client = form_client.save(commit=False)

obj_user.username = form_client.cleaned_data['vat']
obj_user.set_password(form_client.cleaned_data['vat'])
obj_user.is_active = 1
obj_user.save()

obj_client.id_id = obj_user.id
obj_client.save()

# Redirect to Client profile
return HttpResponseRedirect(reverse('client_profile', args={obj_client.id_id}))

# If forms have error show view again with errors
dict['form_user'] = form_user
dict['form_client'] = form_client
return render(request, 'panel/client/form.html', dict)
else:
dict['form_user'] = ClientUserCreateForm(prefix="user")
dict['form_client'] = ClientForm(prefix="client")
return render(request, 'panel/client/form.html', dict)

Forms.py
class ClientUserCreateForm(forms.ModelForm):
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
email = forms.CharField(required=True)

class Meta:
model = User
fields = ('first_name', 'last_name', 'email')

def __init__(self, *args, **kwargs):
super(ClientUserCreateForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['last_name'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['email'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})

def clean(self):
data = self.cleaned_data

first_name = data.get('first_name')
last_name = data.get('last_name')
email = data.get('email')

data['first_name'] = first_name[0].upper() + first_name[1:].lower()
data['last_name'] = last_name[0].upper() + last_name[1:].lower()
data['email'] = email.lower()

return data


class ClientForm(forms.ModelForm):
tags = forms.CharField(widget=forms.Textarea, required=False)

class Meta:
model = Client
fields = ('address', 'zip_code', 'city', 'country', 'forwarding_address',
'forwarding_zip_code', 'forwarding_city', 'forwarding_country',)

def __init__(self, *args, **kwargs):
super(ClientForm, self).__init__(*args, **kwargs)
self.fields['country'].queryset = CountriesChoices.objects.all()
self.fields['forwarding_country'].queryset = CountriesChoices.objects.all()
self.fields['country'].initial = 1
self.fields['address'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['zip_code'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['city'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['country'].widget.attrs.update({
'class': 'form-control',
})


def clean(self):
data = self.cleaned_data

address = data.get('address')
zip_code = data.get('zip_code')
city = data.get('city')
forwarding_address = data.get('forwarding_address')
forwarding_zip_code = data.get('forwarding_zip_code')
forwarding_city = data.get('forwarding_city')

data['address'] = address[0].upper() + address[1:].lower()
data['zip_code'] = zip_code
data['city'] = city[0].upper() + city[1:].lower()

if len(forwarding_address) > 0:
data['forwarding_address'] = forwarding_address[0].upper() + forwarding_address[1:].lower()
else:
data['forwarding_address'] = address[0].upper() + address[1:].lower()

if len(forwarding_zip_code) > 0:
data['forwarding_zip_code'] = forwarding_zip_code
else:
data['forwarding_zip_code'] = zip_code

if len(forwarding_city) > 0:
data['forwarding_city'] = forwarding_city[0].upper() + forwarding_city[1:].lower()
else:
data['forwarding_city'] = city[0].upper() + city[1:].lower()

return data

例如,我将字段“first_name”留空,然后我得到了错误的 Django 页面:“NoneType”对象不可下标(下面的回溯),除了带有错误“此字段是必需的”的表单页面(当我评论干净方法时)。

追溯
Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/panel/client/edit/6/

Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'api',
'client',
'registration',
'avatar',
'filer',
'mptt',
'easy_thumbnails',
'reversion')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\loc\PycharmProjects\pro\core\views.py" in client_edit
192. if form_user.is_valid() and form_client.is_valid():
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\forms\forms.py" in is_valid
184. return self.is_bound and not self.errors
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\forms\forms.py" in errors
176. self.full_clean()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\forms\forms.py" in full_clean
393. self._clean_form()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\forms\forms.py" in _clean_form
417. cleaned_data = self.clean()
File "C:\Users\loc\PycharmProjects\pro\core\forms.py" in clean
421. data['first_name'] = first_name[0].upper() + first_name[1:].lower()

Exception Type: TypeError at /panel/client/edit/6/
Exception Value: 'NoneType' object is not subscriptable

最佳答案

您应该在个人 clean_<fieldname> 中进行类似的验证方法。只有当内容已经有效并填充时才会调用这些。所以:

def clean_firstname(self):
first_name = self.cleaned_data['first_name']
return first_name[0].upper() + first_name[1:].lower()

def clean_last_name(self):
last_name = self.cleaned_data['last_name']
return last_name[0].upper() + last_name[1:].lower()

def clean_email(self):
email = self.cleaned_data['email']
return email.lower()

另请注意,您的名字和姓氏验证可以通过使用 last_name.capitalize() 来简化。 ,它可以完全转换为您想要的格式。

关于Django 形成干净的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36711229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com