在我發佈數據的Django視圖之一中,雖然表單與數據綁定,但表單無效,因此表單無效。我不完全確定爲什麼會發生這種情況。我正在使用的應用程序使用Angular作爲前端,所以我沒有在我的HTML中使用Django的表單類,但它應該沒關係,因爲Django的表單類只是創建HTML小部件?在Django中沒有綁定的表格
的login.html
<div class="account-login" id="login-view">
<div class="card card-half">
<h2 class="text-center">Welcome back!</h2>
<h4 class="text-center">Sign in to your account.</h4>
<div class="alert alert-danger" ng-if="vm.errorMessage">
{{ vm.errorMessage }}
</div>
<form class="form-horizontal" name="form" ng-submit="vm.login(vm.auth)">
{% csrf_token %}
<div class="form-group">
<label for="email" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9 col-md-7">
<input type="email" id="email"
class="form-control"
placeholder="[email protected]"
ng-model="vm.auth.email"
required
hl-focus>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-3 control-label">Password</label>
<div class="col-sm-9 col-md-7">
<input type="password" id="password" name="password"
class="form-control"
placeholder="******"
ng-model="vm.auth.password"
required minlength="6">
<div class="has-warning" ng-if="form.password.$dirty">
<div class="help-block" ng-messages="form.password.$error">
<div ng-message="minlength">Please enter at least six characters.
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-3"></div>
<div class="col-sm-9 col-md-7">
<button type="submit" class="btn btn-block btn-secondary"
ng-disabled="!form.$valid || vm.submitBusy">
Sign in
<span ng-if="vm.submitBusy"><i class="fa fa-circle-o-notch fa-spin"></i></span>
</button>
</div>
</div>
</form>
</div>
</div>
<div class="col-sm-6 col-sm-offset-3">
<p>Forgot your password? Reset it
<a ui-sref="auth.reset">here</a>.</p>
<p>Trying to create a team?
<a ui-sref="auth.join.personal">Sign up</a> to get started.</p>
</div>
形式
class LoginForm(forms.Form):
email = forms.EmailField(max_length=100)
password = forms.CharField(max_length=20)
token = forms.CharField(max_length=20)
def __init__(self, request=None, *args, **kwargs):
self.cached_user = None
self.request = request
kwargs.setdefault('label_suffix', '')
super(LoginForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = self.cleaned_data
if len(self._errors) > 0:
return cleaned_data
else:
email = cleaned_data.get('email')
password = cleaned_data.get('password')
if email is None or password is None:
messages.error(self.request, 'Please enter an email and password.')
return forms.ValidationError("Error")
else:
self.cached_user = authenticate(username=email, password=password)
if self.cached_user is None:
self._errors["password"] = self.error_class(["Password incorrect. Passwords are case sensitive."])
elif not self.cached_user.is_active:
messages.error(self.request,
'This account is inactive. Please check your inbox for our confirmation email, and '
'click the link within to activate your account.')
raise forms.ValidationError("Error")
if not cleaned_data.get('remember_me'):
self.request.session.set_expiry(0)
return cleaned_data
def get_user(self):
return self.cached_user
視圖
def login(request):
# """ -Log in the user if credentials are valid """
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
cleaned_data = form.clean()
account = Account.objects.get(email=cleaned_data['email'], password=cleaned_data['password'])
if cleaned_data['token']:
token = cleaned_data['token']
invite = OrgInvite.objects.get(token=token)
org = Org.objects.get(id=invite.org_id)
if not invite:
raise Exception("Invitation token is invalid.")
if invite.used == True:
raise Exception("Invitation token has already been used.")
org_member = OrgMember.objects.get(account_id=account.id)
if org_member:
raise Exception("Account is already in team.")
else:
org.add_members(account.id, False, invite.is_admin)
invite.used = False
# add_to_welcome(org_id=org.id, account_id=account.id, inviter_id=invite.token)
else:
pass
context = {
'message': 'ok',
'next': '/app/'
}
return composeJsonResponse(200, "", context)
'form.is_valid()'會調用'form.clean()',所以你不應該明確地調用它。爲了在清理完成後獲得表單數據,您可以執行'cleaned_data = form.cleaned_data'。 –
是什麼讓你認爲表單沒有被綁定,或者這就是驗證失敗的原因? –
當我在我的視圖中調試並設置斷點時,表單綁定屬性會變爲false。我將它包含在一張圖片中。它的末端微微滑動,所以它有點難以看清。 – JBT