Mat*_*yne 4 python forms django postgresql
我有一个表单,可以成功加载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
Run Code Online (Sandbox Code Playgroud)
我认为这个错误是通过尝试组合不同的数据类型产生的.但我没有明确这样做.它是通过一些隐藏的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
Run Code Online (Sandbox Code Playgroud)
以下是尝试保存表单数据的视图def:
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')
Run Code Online (Sandbox Code Playgroud)
这是原始视图,它成功加载来自数据库的数据并显示它(然后用户可以更改并保存,以调用前一个视图)(重要:在这种情况下,只调用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
})
Run Code Online (Sandbox Code Playgroud)
有人知道我在哪里混合字符串和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()
Run Code Online (Sandbox Code Playgroud)
编辑#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
Run Code Online (Sandbox Code Playgroud)
Dan*_*man 11
看起来错误在您对该country字段的定义中:您放置max_length="50"而不是max_length=50.
被比较的东西是值的长度,一个int,你定义的最大长度,一个字符串.
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |