gpt4 book ai didi

python - Django 正则表达式验证器不工作 - 错误 500 部署服务器

转载 作者:行者123 更新时间:2023-12-02 02:40:58 24 4
gpt4 key购买 nike

看下面的例子:

class ProjectForm(forms.Form):
client = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Client Name (lowercase)'}),validators=[RegexValidator(r'^[a-z]+$', 'Enter a valid client name (only lowercase letters)')])
stage = forms.ChoiceField(choices=TYPES)
folder = forms.ChoiceField(choices=FOLDERS, required=False, label='')
purpose = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Project Purpose (lowercase; digit optional)'}),validators=[RegexValidator(r'^[a-z-]+[1-9]?$', 'Enter a valid purpose (only lowercase letters, with a single optional digit)')])
computeapi = forms.BooleanField(required=True, initial=True, label='Compute Engine')
deploymentmanapi = forms.BooleanField(required=False, label='Deployment Manager')
storagecompapi = forms.BooleanField(required=False, label='Storage Components')
monitorapi = forms.BooleanField(required=False, label='Monitoring')
loggingapi = forms.BooleanField(required=False, label='Logging')

def clean(self):
form_data = self.cleaned_data
projectname = form_data['client'] + "-" + form_data['stage'] + "-" + form_data['purpose']
client = form_data['client']
purpose = form_data['purpose']
for projectdict in projectdicts:
if projectname == projectdict['name']:
raise ValidationError(projectname + " already exists! Please try another name.")

if not re.match("^[a-z-]+[1-9]?$", purpose):
raise ValidationError(purpose + " does not comply with the Regex parameters.")

if not re.match("^[a-z]+$", client):
raise ValidationError(client + " does not comply with the Regex parameters.")
return form_data

项目名称的 ValidationError 按预期工作得很好。但是,它不适用于 Regex,而是不断抛出 500 错误。我们是否需要专门做一些事情来让 RegexValidator 在类变量中按预期工作?我不认为需要额外的干净代码(如果需要,它就无法正常工作)。

如有任何帮助,我们将不胜感激。

编辑 2020 年 8 月 28 日:根据要求回溯:

Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\user\OneDrive - Company\GCP App Engine\Test-Django-Terraform-Deployments - Copy\webscripts\views.py", line 30, in projectcreation
if form.is_valid():
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 185, in is_valid
return self.is_bound and not self.errors
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 180, in errors
self.full_clean()
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 382, in full_clean
self._clean_form()
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 409, in _clean_form
cleaned_data = self.clean()
File "C:\Users\user\OneDrive - Company\GCP App Engine\Test-Django-Terraform-Deployments - Copy\webscripts\projectform.py", line 90, in clean
projectname = form_data['client'] + "-" + form_data['stage'] + "-" + form_data['purpose']
KeyError: 'client'

projectdicts 字典是根据对 Google Cloud 的 GET 请求生成的全局变量,用于生成所有项目的名称列表:

try:
global projectdicts
projectdicts = cloudresmanv1.projects().list().execute()
projectdicts = projectdicts.get('projects')
except Exception as e:
logging.error(e)

最佳答案

您没有调用父类(super class)的 clean,因此您的 form.clean_data 为空

def clean(self):
form_data = super(ProjectForm, self).clean()
projectname = form_data['client'] + "-" + form_data['stage'] + "-" + form_data['purpose']
client = form_data['client']
purpose = form_data['purpose']
for projectdict in projectdicts:
if projectname == projectdict['name']:
raise ValidationError(projectname + " already exists! Please try another name.")

if not re.match("^[a-z-]+[1-9]?$", purpose):
raise ValidationError(purpose + " does not comply with the Regex parameters.")

if not re.match("^[a-z]+$", client):
raise ValidationError(client + " does not comply with the Regex parameters.")
return form_data

关于python - Django 正则表达式验证器不工作 - 错误 500 部署服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63619099/

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