0
사이트를 개발하는 동안 실제 생산 웹 사이트 앞에 넣고 싶은 "방화벽"로그인 양식을 만들었습니다. 아이디어는 을 시도하고 "나쁜 녀석"을 사이트 밖으로 유지하고 동시에 그들이 사용하고있는 사용자 이름과 암호 인 을 확인하는 것입니다. 내가 가지고있는 문제는 잘못된 사용자 이름/암호 쌍을 입력하면 양식의 오류 메시지 이 표시되지 않습니다. 나는 나의 목적을 위해 그것이 어떤 오류 메시지를 전혀 표시하지 않는 것이 더 나은 일지 모르지만 나는 여전히 문제가 무엇인지 이해하고있는 을 알고 싶습니다. 누구든지 내가 뭘 잘못하고 있는지 알 수 있니?장고 양식 오류가 표시되지 않습니다.
감사합니다.
# views.py
import logging
logger = logging.getLogger(__name__)
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.views import login
from django.http import HttpResponseRedirect
def firewall_login(request, *args, **kwargs):
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
username = request.POST['username']
password = request.POST['password']
if form.is_valid():
fw_username = form.cleaned_data['username']
fw_password = form.cleaned_data['password']
user = authenticate(username=fw_username, password=fw_password)
if user is not None:
if user.is_active:
login(request, user)
logger.info("User '%s' logged in." % fw_username)
return HttpResponseRedirect("/accounts/profile/")
else:
logger.info("User '%s' tried to log in to disabled account." % fw_username)
return HttpResponseRedirect("/accounts/disabled/")
else:
logger.info("User '%s' tried to log in with password '%s'." % (username, password))
form = AuthenticationForm(request) # Display bound form
else:
form = AuthenticationForm() # Display unbound form
return render(request, "registration/login.html", {"form": form,})
# login.html
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p class="alert alert-error">Sorry, that's not a valid username or password</p>
{% endif %}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for field in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<form action="" method="post">
{% csrf_token %}
<p><label for="username">Username:</label>{{ form.username }}</p>
<p><label for="password">Password:</label>{{ form.password }}</p>
<input type="hidden" name="next" value="{{ next|escape }}" />
<input class="btn btn-primary" type="submit" value="login" />
</form>
{% endblock %}
합니다. 감사! – William