Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. ...
  2. <button type="button" class="btn btn-secondary btn-sm" data-toggle="modal" data-target="#loginModal" id="login_modal_trigger">Log In</button>
  3.  
  4. {% include 'registration/login.html' with form=form %}
  5. ...
  6.  
  7. <div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel" aria-hidden="true">
  8. <div class="modal-dialog" role="document">
  9. <div class="modal-content">
  10. <div class="modal-header">
  11. <h5 class="modal-title" id="exampleModalLabel">Log In</h5>
  12. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  13. <span aria-hidden="true">&times;</span>
  14. </button>
  15. </div>
  16. <div class="modal-body">
  17.  
  18. <div id="content-container" class="container p-none">
  19. <div class="lgn-container col-lg-8">
  20. <form id="login-form" method="post"
  21. action="">
  22. {% csrf_token %}
  23. <table class="table">
  24. <tr>
  25. <td><label for="id_username">Username</label></td>
  26. <td><input id="id_username" name="username"
  27. type="text" class="form-control"></td>
  28. </tr>
  29. <tr>
  30. <td><label for="id_password">Password</label></td>
  31. <td><input id="id_password" name="password"
  32. type="password" class="form-control"></td>
  33. </tr>
  34. </table>
  35. {% if form.errors %}
  36. <p class=" label label-danger">
  37. Your username and password didn't match.
  38. Please try again.
  39. </p>
  40. {% endif %}
  41.  
  42. <input type="submit" id="ajax_form_submit" value="Login"
  43. class="btn btn-primary pull-right" />
  44. </form>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51.  
  52. $(document).ready(function() {
  53. $("#login_form").submit(function(event) {
  54. jQuery.ajax({
  55. "data": $(this).serialize(),
  56. "type": $(this).attr("method"),
  57. "url": "{% url 'login_user' %}",
  58. "success": function(response) {
  59. // switch(response.code) {
  60. // // Do what you want with the received response
  61. // }
  62. console.log(response);
  63. if (response.code == 0) {
  64. console.log("fail");
  65. }
  66. }
  67. });
  68. event.preventDefault();
  69. });
  70. });
  71.  
  72. ...
  73. urlpatterns = [
  74. ...
  75. url(r'^login/$', views.login_user, name='login_user'),
  76. ...
  77. ]
  78. ...
  79.  
  80. def login_user(request):
  81. username = request.POST['username']
  82. password = request.POST['password']
  83. user = authenticate(request, username=username, password=password)
  84. if user is not None:
  85. login(request, user)
  86. # Redirect to a success page.
  87. return HttpResponseRedirect(reverse('webday_main:detail', args=(username,)))
  88. else:
  89. # Return an 'invalid login' error message.
  90. response = {'code': 0}
  91. return HttpResponse(response, content_type='application/json')
  92. # return HttpResponse(status=404)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement