Guest User

Untitled

a guest
May 4th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. # views.py
  2. import logging
  3. logger = logging.getLogger(__name__)
  4. from django.contrib.auth import authenticate
  5. from django.contrib.auth.forms import AuthenticationForm
  6. from django.contrib.auth.views import login
  7. from django.http import HttpResponseRedirect
  8.  
  9. def firewall_login(request, *args, **kwargs):
  10. if request.method == "POST":
  11. form = AuthenticationForm(request, data=request.POST)
  12. username = request.POST['username']
  13. password = request.POST['password']
  14. if form.is_valid():
  15. fw_username = form.cleaned_data['username']
  16. fw_password = form.cleaned_data['password']
  17. user = authenticate(username=fw_username, password=fw_password)
  18. if user is not None:
  19. if user.is_active:
  20. login(request, user)
  21. logger.info("User '%s' logged in." % fw_username)
  22. return HttpResponseRedirect("/accounts/profile/")
  23. else:
  24. logger.info("User '%s' tried to log in to disabled account." % fw_username)
  25. return HttpResponseRedirect("/accounts/disabled/")
  26. else:
  27. logger.info("User '%s' tried to log in with password '%s'." % (username, password))
  28. form = AuthenticationForm(request) # Display bound form
  29. else:
  30. form = AuthenticationForm() # Display unbound form
  31. return render(request, "registration/login.html", {"form": form,})
  32.  
  33. # login.html
  34. {% extends "base.html" %}
  35. {% block content %}
  36.  
  37. {% if form.errors %}
  38. <p class="alert alert-error">Sorry, that's not a valid username or password</p>
  39. {% endif %}
  40.  
  41. {% if form.errors %}
  42. {% for field in form %}
  43. {% for error in field.errors %}
  44. <div class="alert alert-error">
  45. <strong>{{ error|escape }}</strong>
  46. </div>
  47. {% endfor %}
  48. {% endfor %}
  49. {% for field in form.non_field_errors %}
  50. <div class="alert alert-error">
  51. <strong>{{ error|escape }}</strong>
  52. </div>
  53. {% endfor %}
  54. {% endif %}
  55.  
  56. <form action="" method="post">
  57. {% csrf_token %}
  58. <p><label for="username">Username:</label>{{ form.username }}</p>
  59. <p><label for="password">Password:</label>{{ form.password }}</p>
  60. <input type="hidden" name="next" value="{{ next|escape }}" />
  61. <input class="btn btn-primary" type="submit" value="login" />
  62. </form>
  63.  
  64. {% endblock %}
  65.  
  66. def firewall_login(request, *args, **kwargs):
  67. if request.method == "POST":
  68. form = AuthenticationForm(request, data=request.POST)
  69. username = request.POST['username']
  70. password = request.POST['password']
  71. if form.is_valid():
  72. fw_username = form.cleaned_data['username']
  73. fw_password = form.cleaned_data['password']
  74. user = authenticate(username=fw_username, password=fw_password)
  75. if user is not None:
  76. if user.is_active:
  77. login(request, user)
  78. logger.info("User '%s' logged in." % fw_username)
  79. return HttpResponseRedirect("/accounts/profile/")
  80. else:
  81. logger.info("User '%s' tried to log in to disabled account." % fw_username)
  82. return HttpResponseRedirect("/accounts/disabled/")
  83. else:
  84. logger.info("User '%s' tried to log in with password '%s'." % (username, password))
  85. else:
  86. form = AuthenticationForm() # Display unbound form
  87. return render(request, "registration/login.html", {"form": form,})
Add Comment
Please, Sign In to add comment