Ich habe eine benutzerdefinierte Django Login-Seite. Ich möchte eine Ausnahme auslösen, wenn der Benutzername oder das Passwort leer sind. Wie kann ich das machen?Django benutzerdefinierte Login-Seite
Mein view.py Login-Methode:
def user_login(request):
context = RequestContext(request)
if request.method == 'POST':
# Gather the username and password provided by the user.
# This information is obtained from the login form.
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
print("auth",str(authenticate(username=username, password=password)))
if user:
# Is the account active? It could have been disabled.
if user.is_active:
login(request, user)
return HttpResponseRedirect('/')
else:
return HttpResponse("xxx.")
else:
# Bad login details were provided. So we can't log the user in.
print ("Invalid login details: {0}, {1}".format(username, password))
return HttpResponse("Invalid login details supplied.")
else:
return render_to_response('user/profile.html', {}, context)
habe ich versucht, diese und es hat nicht funktioniert: Dies ist forms.py
def clean_username(self):
username = self.cleaned_data.get('username')
if not username:
raise forms.ValidationError('username does not exist.')
Können Sie bitte die Frage mit der richtigen Vertiefung aktualisieren – Sayse
definieren nur 'clean_username (Selbst-): Wenn nicht self.cleaned_data [ 'username']: Validation ('')' Methode in Form entspricht. –
Hallo, ich lege es nur zum Testen, xxx ist eine HTML-Seite. – chazefate