2014-12-06 69 views
0

我已经构建了一个“防火墙”登录表单,我想在我的 实际生产网站前放置,而我开发该网站。我们的想法是尝试 ,并将“坏人”排除在网站之外,同时了解他们使用的用户名和密码是什么 。我遇到的问题是 ,如果我输入无效的用户名/密码对,我的表单的错误信息 不会显示。我意识到,对我而言,它可能是 更好,根本不显示任何错误消息,但我仍然希望 了解问题所在。任何人都可以看到我做错了什么?Django表格错误将不会显示

谢谢。

# 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 %} 

回答

1

这是因为你传递了新的表单实例。验证发生在is_valid呼叫。

所以,只要删除form = AuthenticationForm(request)else块:

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)) 
else: 
    form = AuthenticationForm() # Display unbound form 
return render(request, "registration/login.html", {"form": form,}) 
+0

这是问题。谢谢! – William 2014-12-06 21:06:05