我正在创建一个自定义用户表单,用户可以在其中更改某些详细信息。它有两个错误我无法理解问题是什么。
第一个问题是表单没有填写用户详细信息。而且似乎是因为self
在表单初始化时为空,为什么?
第二个问题是,当我提交此表单时,它会抱怨一些KeyError,我也没有。我已经粘贴了代码和堆栈跟踪。
Environment:
Request Method: POST
Request URL: http://localhost:8000/settings/
Django Version: 1.4
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'social_auth')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\contrib\auth\decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "C:\xampp\htdocs\tddd27_project\contact\views.py" in settings
47. if form.is_valid():
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\forms\forms.py" in is_valid
124. return self.is_bound and not bool(self.errors)
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\forms\forms.py" in _get_errors
115. self.full_clean()
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\forms\forms.py" in full_clean
270. self._clean_fields()
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\forms\forms.py" in _clean_fields
290. value = getattr(self, 'clean_%s' % name)()
File "c:\tools\python27\lib\site-packages\django-1.4-py2.7.egg\django\contrib\auth\forms.py" in clean_password
122. return self.initial["password"]
Exception Type: KeyError at /settings/
Exception Value: 'password'
def settings(request):
if request.method == 'POST':
form = UserProfileForm(request.POST)
if form.is_valid():
form.save()
return HttpResponse("Change success")
else:
form = UserProfileForm(instance=request.user)
return render_to_response('settings.html',{'form': form}, context_instance=RequestContext(request))
class UserProfileForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name')
exclude = ('password',)
def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args, **kwargs)
if self.instance and self.instance.pk:
self.fields['username'] = self.instance.username
self.fields['first_name'] = self.instance.first_name
self.fields['last_name'] = self.instance.last_name
{% extends "base.html" %}
{% block title %}
Settings
{% endblock title %}
{% block content %}
{{ user.username }}
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit">
</form>
{% endblock content %}
答案 0 :(得分:5)
关于你的第二个问题,我今天也遇到了同样的问题,同时从Django 1.3更新到Django 1.4。我有一个像你一样的自定义表格:
class UserProfileForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', )
我通过添加clean_password
方法解决了这个问题:
class UserProfileForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', )
def clean_password(self):
return "" # This is a temporary fix for a django 1.4 bug
答案 1 :(得分:2)
第一个问题是表单没有填写用户详细信息。 而且似乎是因为表单初始化时self是空的, 为什么呢?
这是启动一个新的空白模型形式:UserProfileForm()
或UserProfileForm(request.POST)
这是启动绑定到用户实例的模型:UserProfileForm(instance=request.user)
或UserProfileForm(request.POST, instance=request.user)
。
如果您不完全理解这一点,请阅读documentation about ModelForm usage。
在您的视图中,您将实例化新的空白表单,而不是绑定到request.user实例的表单。例如此UserProfileForm(request.POST)
应为UserProfileForm(request.POST, instance=request.user)
然后,这绝对没有必要:
if self.instance and self.instance.pk:
self.fields['username'] = self.instance.username
self.fields['first_name'] = self.instance.first_name
self.fields['last_name'] = self.instance.last_name
这不能解决KeyError问题,但您应该更改:
exclude = ('password')
要:
exclude = ('password',)
请注意,在第二种情况下,exclude是一个元组。在python shell中查看此示例:
In [1]: ('foo')
Out[1]: 'foo'
In [2]: ('foo',)
Out[2]: ('foo',)
至于你的KeyError,我真的很不确定。我可以see the problem但我还没有破解它应该如何工作。无论如何,如果排除密码,则不应该运行此功能。
答案 2 :(得分:0)
class UserProfileForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', )
def clean_password(self):
return self.clean_password
这将解决问题。希望这会有所帮助。