Mam niestandardową stronę logowania Django. Chcę rzucić wyjątek, gdy pola nazwy użytkownika lub hasła są puste. Jak mogę to zrobić?Django niestandardowa strona logowania
Moja view.py dziennik w metodzie:
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)
próbowałem tego i to nie działa: To forms.py
def clean_username(self):
username = self.cleaned_data.get('username')
if not username:
raise forms.ValidationError('username does not exist.')
Czy należy zaktualizować pytanie z poprawną wcięcia – Sayse
wystarczy zdefiniować 'clean_username (self): jeśli nie self.cleaned_data [ 'login']: ValidationError ('')' metody w odpowiednim formularzu. –
Witam, umieściłem to tylko do testowania, xxx to strona html. – chazefate