我是Django的新手,我不知道为什么在/ id_finder / forms /上每次遇到此AttributeError时,每次尝试提交表单时,“ NoneType”对象都没有属性“ strip”。有人可以看下面我的代码吗,也许我做的不正确。
forms.py
class UserForm(forms.Form):
first_name = forms.CharField(max_length = 15,required = True)
last_name = forms.CharField(max_length = 15,required = True)
email = forms.EmailField(max_length = 50,required = True)
phone_number = forms.CharField(max_length = 14,required = True)
password = forms.CharField(max_length = 30,widget = forms.PasswordInput(),required = True)
region = forms.ModelChoiceField(queryset=Region.objects.all(),empty_label = None,required = True)
district = forms.ModelChoiceField(queryset=District.objects.all(),empty_label = None,required = True)
models.py
class User(models.Model):
FirstName = models.CharField(max_length = 15)
LastName = models.CharField(max_length = 15)
PhoneNumber = models.CharField(max_length = 14)
Email = models.EmailField(max_length = 50)
Password = models.CharField(max_length = 30)
Date = models.DateTimeField(auto_now_add = True)
Region = models.OneToOneField('Region', on_delete = models.PROTECT)
District = models.OneToOneField('District', on_delete = models.PROTECT)
def __str__(self):
return "{0} {1}".format(self.FirstName,self.LastName)
class Region(models.Model):
RegionName = models.CharField(max_length = 20)
def __str__(self):
return self.RegionName
class District(models.Model):
DistrictName = models.CharField(max_length = 30)
Region = models.ForeignKey(Region, on_delete = models.PROTECT)
def __str__(self):
return self.DistrictName
views.py
def form(request):
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
obj = User() # create a new model object
#process form data
obj.first_name = form.cleaned_data['first_name']
obj.last_name = form.cleaned_data['last_name']
obj.email = form.cleaned_data['email']
obj.phone_number = form.cleaned_data['phone_number']
obj.password = form.cleaned_data['password']
obj.region = form.cleaned_data['region']
obj.district = form.cleaned_data['district']
#save the data
obj.save()
return HttpResponseRedirect("Information saved successfully")
else:
form = UserForm()
return render(request,'id_finder/form.html',{'form':form})
跟踪:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py" in cmd_query
395. raw_as_string=raw_as_string)
During handling of the above exception (Column 'Region_id' cannot be null), another exception occurred:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/django/base.py" in _execute_wrapper
168. return method(query, args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor_cext.py" in execute
266. raw_as_string=self._raw_as_string)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py" in cmd_query
398. sqlstate=exc.sqlstate)
During handling of the above exception (1048 (23000): Column 'Region_id' cannot be null), another exception occurred:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
99. return super().execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
67. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
76. return executor(sql, params, many, context)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute
84. return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/django/base.py" in execute
218. return self._execute_wrapper(self.cursor.execute, query, new_args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/django/base.py" in _execute_wrapper
174. utils.IntegrityError(err.msg), sys.exc_info()[2])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/six.py" in reraise
683. raise value.with_traceback(tb)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/django/base.py" in _execute_wrapper
168. return method(query, args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor_cext.py" in execute
266. raw_as_string=self._raw_as_string)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py" in cmd_query
398. sqlstate=exc.sqlstate)
During handling of the above exception (Column 'Region_id' cannot be null), another exception occurred:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor_cext.py" in statement
606. return self._executed.strip().decode('utf8')
During handling of the above exception ('NoneType' object has no attribute 'strip'), another exception occurred:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/mohameddiaby/Desktop/Project/Lost_and_Found/id_finder/views.py" in form
28. obj.save()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/base.py" in save
741. force_update=force_update, update_fields=update_fields)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/base.py" in save_base
779. force_update, using, update_fields,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/base.py" in _do_insert
908. using=using, raw=raw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1335. cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
103. sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/django/operations.py" in last_executed_query
127. return force_text(cursor.statement, errors='replace')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/django/base.py" in __getattr__
230. return getattr(self.cursor, attr)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor_cext.py" in statement
608. return self._executed.strip()
编辑:我注意到的另一件事是,查询中有空值,该值将信息从表单发送到数据库。我不明白为什么
跟踪:
buffered
False
query
(b'INSERT INTO `id_finder_user` (`FirstName`, `LastName`, `PhoneNumber`, `Email'
b"`, `Password`, `Date`, `Region_id`, `District_id`) VALUES ('', '', '', '', '"
b"', '2019-07-07 11:56:25.206852', NULL, NULL)")
raw
False
raw_as_string
False
self
<mysql.connector.connection_cext.CMySQLConnection object at 0x10cb61c18>