daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Nov 6th, 2018 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #kustomowy emailauthbackend
  2.  
  3. from django.contrib.auth.models import User
  4.  
  5. class EmailAuthBackend():
  6.  
  7.     def authenticate(self,request,username,password):
  8.         try:
  9.             user = User.objects.get(email=username)
  10.             success = user.check_password(password)
  11.             if success:
  12.                 return user
  13.         except User.DoesNotExist:
  14.              try:
  15.                 user = User.objects.get(username=username)
  16.                 if user.check_password(password):
  17.                     return user
  18.              except User.DoesNotExist:
  19.                 return None
  20.  
  21.     def get_user(self,uid):
  22.         try:
  23.             return User.objects.get(pk=uid)
  24.         except:
  25.             return None
  26.  
  27.     def has_perm(self, user_obj, perm, obj=None):
  28.         if user_obj.username == settings.ADMIN_LOGIN:
  29.             return True
  30.         else:
  31.             return False
  32.  
  33.  
  34. #login.html
  35.     {% extends 'home/base.html' %}
  36.  
  37. {% block title %}Login{% endblock %}
  38.  
  39.  
  40.  
  41. {% block content %}
  42.  
  43. <h1> Log in </h1>
  44. <form method="post" action="{% url 'answer:login_view' %}">
  45. {% csrf_token %}
  46.   <table>
  47.   <tr>
  48.    <td>{{ form.email.label_tag }}</td>
  49.    <td>{{ form.email }}</td>
  50.   </tr>
  51.   <tr>
  52.    <td>{{ form.password.label_tag }}</td>
  53.    <td>{{ form.password }}</td>
  54.   </tr>
  55.   </table>
  56.  
  57.   <input type="submit" value="login" />
  58.   <input type="hidden" name="next" value="{{ next }}" />
  59. </form>
  60. {% endblock %}
  61.  
  62.  
  63. widok
  64.  
  65.  
  66. def login_view(request):
  67.     from django.contrib.auth.forms import AuthenticationForm
  68.     form = LoginForm(request.POST or None)
  69.     if request.method == 'POST' and form.is_valid():
  70.         user = authenticate(email=user, password=password)
  71.         login(request, user)
  72.         return redirect('index')
  73.     else:
  74.         return HttpResponseRedirect('Error')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top