【发布时间】:2018-12-28 15:13:14
【问题描述】:
我正在尝试设置一个页面以允许用户更改他们的名字和姓氏。问题是我不想在表单中包含密码,但如果我不包含密码,用户将无法更新信息。
我从 UserChangeForm 创建了我自己的表单,只包含名字、姓氏和密码:
class UserForm(UserChangeForm):
password = auth_forms.ReadOnlyPasswordHashField(label="Password",
help_text="Para cambiar la contraseña por favor pulsa "
"<a href=\"change-password/\">en este link</a>.")
class Meta:
model = User
fields = (
'first_name',
'last_name',
'password'
)
我的 HTML 很简单:
<form method="post" >
{% csrf_token %}
{{ user_form.as_p }}
<button type="submit">Actualizar</button>
</form>
我曾考虑在 HTML 中仅包含类似以下内容的输入:
> <div class="form-group">
> <input type="text" class="form-control" name="first_name" id="inputFirstName" placeholder="First_name">
> </div>
但它不显示名字的当前值。
我能做什么?
非常感谢
编辑:
观点是:
@login_required
@transaction.atomic
def update_profile(request):
if request.method == 'POST':
user_form = UserForm(request.POST, instance=request.user)
#profile_form = ProfileForm(request.POST, instance=request.user.profile)
#if user_form.is_valid() and profile_form.is_valid():
if user_form.is_valid():
user_form.save()
#profile_form.save()
return redirect('/user')
else:
#print(user_form.errors, profile_form.errors)
print(user_form.errors)
elif request.method == "GET":
user_form = UserForm(instance=request.user)
#profile_form = ProfileForm(instance=request.user.profile)
#return render(request, 'user_data.html', {'user_form': user_form, 'profile_form': profile_form})
return render(request, 'user_data.html', {'user_form': user_form})
错误:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/user
Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
'portfolios',
'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\contextlib.py" in inner
52. return func(*args, **kwds)
File "C:\Users\AlbertoCarmona\Desktop\ibotics\chimpy\profiles\views.py" in update_profile
91. if user_form.is_valid():
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py" in is_valid
179. return self.is_bound and not self.errors
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py" in errors
174. self.full_clean()
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py" in full_clean
376. self._clean_fields()
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py" in _clean_fields
397. value = getattr(self, 'clean_%s' % name)()
File "C:\Users\AlbertoCarmona\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\forms.py" in clean_password
150. return self.initial["password"]
Exception Type: KeyError at /user
Exception Value: 'password'
【问题讨论】:
-
您的问题是什么?为什么要对 HTML 进行硬编码?
-
问题是我不想在表单中包含密码但是如果我不包含它,用户将无法更新信息,所以我想到了硬编码html
-
如果您不想要表单中的密码,为什么要将其包含在字段列表中?
-
因为如果我不这样做,用户无法更新信息
-
为什么不呢?会发生什么?