【发布时间】:2013-11-29 16:40:13
【问题描述】:
我觉得使用Django 的AuthenticationForm很方便 但是由于我们客户端的用户名有点长好记,而且我们的客户端数量不多,所以我们想在登录页面使用选择小部件。所以我使用下面的代码来实现它。 有效。
from django.contrib.auth.forms import AuthenticationForm
class SignInForm(AuthenticationForm):
username = forms.ModelChoiceField(queryset=User.objects.all())
在views.py中:
def post(self, request):
form = SignInForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return HttpResponseRedirect(reverse('account:signin'))
return render(request, 'account/sign-in.html', {'form': form})
但是,我在文档中找不到任何类似的用法。所以我不确定这样做是否正确。
不知道是不是和ModelForm类似: https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
我想知道这里发生了什么。我在 SignInForm 中声明的用户名是否会阻止 AuthenticationForm 像 ModelForm 这样生成其用户名?但是 AuthenticationForm 成功地对待我声明的用户名就像它应该生成的用户名一样?
【问题讨论】: