我有一个表单,它成功地从PostgreSQL数据库加载了现有数据,并填写了字段,这样用户就可以在可编辑的表单中看到自己的信息。但当我按下"保存更改"按钮(加载一个视图,该视图被编程为将数据保存回同一数据库条目)时,我会收到以下错误:
(为了便于阅读,我将此处的实际URL更改为简单的"/PATH/")
TypeError at /accounts/update_profile/
unorderable types: int() > str()
Request Method: POST
Request URL: http://www.limbs.ca/accounts/update_profile/
Django Version: 1.7.7
Exception Type: TypeError
Exception Value:
unorderable types: int() > str()
Exception Location: /home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/validators.py in <lambda>, line 279
Python Executable: /usr/local/bin/python3
Python Version: 3.4.1
Python Path:
['/PATH/lib/python3.4/Django-1.7.7-py3.4.egg',
'/PATH',
'/PATH/myproject',
'/PATH/lib/python3.4',
'/usr/local/lib/python34.zip',
'/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-linux',
'/usr/local/lib/python3.4/lib-dynload',
'/usr/local/lib/python3.4/site-packages']
Server time: Sun, 5 Apr 2015 18:46:17 +0000
Traceback Switch to copy-and-paste view
/PATH/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
...
▶ Local vars
/PATH/myproject/home/views.py in update_profile
if profile_form.is_valid():
...
▶ Local vars
我认为这个错误是由于试图组合不同的数据类型而产生的。但我并没有明确这么做。这是通过一些隐藏的Django过程完成的,我无法发现它是什么
这是forms.py中的表单类:
class ProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('profile_image', 'country', 'description')
def save(self, commit=True):
profile = super(ProfileForm, self).save(commit=True)
profile.profile_image = self.cleaned_data['profile_image']
profile.country = self.cleaned_data['country']
profile.description = self.cleaned_data['description']
if commit:
profile.save()
return profile
这是试图保存表单数据的视图定义:
def update_profile(request):
selected_user_id = request.user.id
selected_user_profile = UserProfile.objects.filter(user_id=selected_user_id)[0]
profile_form = ProfileForm(request.POST, instance = selected_user_profile)
if profile_form.is_valid():
profile_form.save()
return HttpResponseRedirect('/accounts/profile')
这是一个原始视图,它成功地从DB加载数据并显示它(然后用户可以更改和保存它,以调用以前的视图)(重要信息:在这种情况下,只调用ELSE内容):
def user_profile(request):
if request.method == 'GET' and 'profile_id' in request.GET:
profile_id = request.GET.get('profile_id')
selected_user_profile = UserProfile.objects.get(pk=profile_id)
selected_user_id = selected_user_profile.user_id
selected_user = User.objects.get(pk=selected_user_id)
return render(request, 'reg/other_user_profile.html', {
'profile': selected_user_profile, 'selected_user': selected_user
})
else:
selected_user_id = request.user.id
selected_user = request.user
selected_user_profile = UserProfile.objects.filter(user_id=selected_user_id)[0]
profile_form = ProfileForm()
return render(request, 'reg/current_user_profile.html', {
'profile': selected_user_profile, 'selected_user': selected_user, 'profile_form': profile_form
})
有人知道我在哪里把字符串和int混合在一起吗?我真的不知道!
编辑:这是"复制粘贴"回溯:(加上它的实时链接):http://dpaste.com/04YYD16
Environment:
Request Method: POST
Request URL: http://www.limbs.ca/accounts/update_profile/
Django Version: 1.7.7
Python Version: 3.4.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testapp',
'home')
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')
Traceback:
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/pattmayne/webapps/limbs_006/myproject/home/views.py" in update_profile
129. if profile_form.is_valid():
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/forms.py" in is_valid
162. return self.is_bound and not bool(self.errors)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/forms.py" in errors
154. self.full_clean()
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/forms.py" in full_clean
355. self._post_clean()
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/models.py" in _post_clean
422. self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/base.py" in full_clean
990. self.clean_fields(exclude=exclude)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/base.py" in clean_fields
1032. setattr(self, f.attname, f.clean(raw_value, self))
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/fields/__init__.py" in clean
512. self.run_validators(value)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/fields/__init__.py" in run_validators
464. v(value)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/validators.py" in __call__
245. if self.compare(cleaned, self.limit_value):
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/validators.py" in <lambda>
279. compare = lambda self, a, b: a > b
Exception Type: TypeError at /accounts/update_profile/
Exception Value: unorderable types: int() > str()
编辑#2:型号定义:
class UserProfile(models.Model):
country = models.CharField(default="Earth", max_length="50")
description = models.TextField(default="One more happy user...")
friends = models.ManyToManyField("self", blank=True, null=True)
profile_image = models.FileField(upload_to='prof_img/user/%Y/%m/%d', default='prof_img/user/default.jpg')
user = models.OneToOneField(User)
def __str__(self):
return user.username
看起来错误出现在country
字段的定义中:您放置了max_length="50"
而不是max_length=50
。
要比较的是值的长度,一个int,和你定义的最大长度,字符串。