Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. views.py
  2.  
  3. # handles logins
  4. def login(request):
  5.     c = {}
  6.     c.update(csrf(request))
  7.  
  8.     if request.method == 'POST':
  9.         #request.session:set_test_cookie()
  10.         username = request.POST.get('username', '')
  11.         password = request.POST.get('password', '')
  12.         login_form = AuthenticationForm(request, request.POST)
  13.         if login_form.is_valid():
  14.             if request.is_ajax:
  15.                 user = auth.authenticate(username=username, password=password)
  16.                 #user = django_login(request, login_form.get_user())
  17.                 if user is not None:
  18.                     auth.login(request, user)
  19.                     return render(request, 'login/admin.html', c)
  20.                     #return HttpResponse(request.REQUEST.get('next', '/'))
  21.         return HttpResponseForbidden()
  22.     #return HttpResponse('asd')
  23.     return render(request, 'login/login.html', c)
  24.  
  25.  
  26. test.js
  27.  
  28.  $(window).on('load', function (){
  29.     // Submit post on submit
  30.  
  31.     $('#login_form').on('submit', function(event){
  32.         event.presentDefault()
  33.         alert('neger');
  34.         //var request_url = document.getElementById('next').value
  35.         $.ajax({
  36.             type:"POST",
  37.             //url: $(this).attr('action'),
  38.             url: '/login',
  39.             data: $('#login_form').serialize(),
  40.             success: function(response){
  41.                     //window.location = response
  42.             },
  43.             error: function(xhr, ajaxOptions, thrownError){
  44.                 alert('login failed')
  45.             },
  46.         });
  47.     });
  48. });
  49.  
  50.  
  51. login.html
  52.  
  53. <!-- The page where admin logs in -->
  54. {% include 'login/_header.html' %}
  55.  
  56. {% block content %}
  57.  
  58.   {% if form.errors %}
  59.     <p class="error">Sorry, thats not a valid username or password</p>
  60.   {% endif %}
  61. <!-- action="/auth/" -->
  62.   <form id="login_form" method="post">
  63.     {% csrf_token %}
  64.     <label for="username">User name:</label>
  65.     <input type="text" name="username" value="" id="username">
  66.     <label for="password">Password:</label>
  67.     <input type="password" name="password" value="" id="password">
  68.  
  69.     <input type="submit" />
  70.   </form>
  71.  
  72.  
  73.  
  74. {% endblock %}
  75. {% include 'login/_footer.html' %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement