我正在尝试创建一个可以插入应用程序的便携式身份验证系统,而我重新实现它的每个不同的应用程序都有相同的问题。
1 - 有时最近登录的用户在新用户尝试注册时会在电子邮件地址字段中获取sn,如下所示
2-有时新用户注册并注销,但表单会将旧用户的电子邮件地址和密码放在相应的字段中,当然我希望如果用户已注销,表单将为空白
3-总是在重新加载时填写最后使用的密码
我只是希望表单在重新加载
时完全清除How to clear form fields after a submit in Django
我已经尝试了类似问题的所有3个解决方案,我在保存有效的问题后重新实例化了from,制作了request.POST的副本并使用了它,我已经重定向开始了。这是我的表格
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'email', 'password')
在views.py中
def register(request):
context = RequestContext(request)
registered = False
user_form = UserForm()
if request.method == 'POST':
pDict = request.POST.copy()
form = UserForm(pDict)
if form.is_valid():
user = form.save()
user.set_password(user.password)
user.save()
user_form = UserForm()
registered = True
username = pDict['username']
password = pDict['password']
user = authenticate(username=username, password=password)
login(request, user)
#locals isn't working? won't print user
return HttpResponseRedirect('/url/')
else:
print user_form.errors
template_name = 'accounts/register.html'
user_form = UserForm()
response = TemplateResponse(request, 'accounts/register.html', locals())
return response
谢谢