0

i tried alot to solve my problem but i cant code works fine i guess but it never finds similar rows in database or i cant login .. i copied the email backend from some site i havent written it by my own so i cant find errors in it if there are cause am learning python web development.

views.py

  from django.shortcuts import render
from django.contrib.auth import authenticate
from Play.models import user
from django.contrib.auth.models import User
from django.contrib.auth.models import auth
from django.shortcuts import redirect
from django.shortcuts import HttpResponse
from django.contrib.auth.hashers import make_password

def JoinRequest(request):

    if request.method == 'POST':
        fullname = request.POST['fullname']
        email = request.POST['email']
        phone = request.POST['phone']
        password = request.POST['password']
        cpassword = request.POST['cpassword']

        #encpass = make_password(password)

        def get_client_ip(request):
            x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
            if x_forwarded_for:
                ip = x_forwarded_for.split(',')[-1].strip()
            else:
                ip = request.META.get('REMOTE_ADDR')
            return ip

        if password != cpassword:
            return HttpResponse('Password Not Matching To Confirm Password Value..')

        else:
            senddata = user(fullname=fullname, email=email, phone=phone, password=password , ipaddress=get_client_ip(request))
            senddata.save()


            return HttpResponse('')



def Join_View(request):
    return render(request, 'Join.html', {})

def login_view(request):
    return render(request, 'Login.html', {})


def LoginREQUEST(request):

    if request.method == 'POST':
        username = request.POST['email']
        password = request.POST['password']

        user = auth.authenticate(username=username, password=password)

        if user is not None:

            auth.login(request, user)
            return HttpResponse('DONE')
        else:
            return HttpResponse("NONE")

SETTINGS.py

AUTHENTICATION_BACKENDS = [
    'Accounts.EmailAuthentication.EmailOrUsernameModelBackend',
]

EmailAuthentication.py

   from django.contrib.auth import get_user_model
from django.http import HttpResponse
from django.contrib.auth.backends import ModelBackend as _ModelBackend

User = get_user_model()


class EmailOrUsernameModelBackend(_ModelBackend):
    """ Authenticate user by username or email """

    def authenticate(self, username=None, password=None):
        try:
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user
            else:
                return None
        except User.DoesNotExist:
            return None

    def get_user(self, user_id=None):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

PLEASE EGNORE THE INDENTATION HERE ITS FINE IN CODE.

THE INSERTING OF DATA IS WoRKING PERFECTLY.

  • "PLEASE EGNORE THE INDENTATION HERE ITS FINE IN CODE"—no. Please _fix_ your indentation here. [Edit] your question, paste the code into it, then select it and click the `{}` button or press Ctrl+K. We can't be expected to guess which errors are relevant and which aren't. (Also, please don't SHOUT. If you need to emphasize something you can [do it with Markdown](https://stackoverflow.com/help/privileges/comment).) – ChrisGPT was on strike Jun 07 '20 at 14:40
  • @Chris I EDITED please have a look , help me please – utkarsh rai Jun 07 '20 at 15:09
  • Why are you writing your own authentication views in the first place? – ChrisGPT was on strike Jun 07 '20 at 15:14
  • Aside: code formatting is important. There is an [official style guide for Python](https://www.python.org/dev/peps/pep-0008/), but more importantly your _own_ code should be consistent. Your four view functions are _all_ formatted differently: one uses `StudlyCaps`, one `Adds_Underscores`, one uses `snake_case`, and one uses `StudlyWithALLCAPS`. This is distracting and confusing. Later, when you want to refer to one of your own functions, will _you_ want to deal with this? If you're consistent you will have a _much_ better time. – ChrisGPT was on strike Jun 07 '20 at 15:18
  • ill do that but did you find any errors why the code is not working? @Chris – utkarsh rai Jun 07 '20 at 15:40
  • Well, as I said in my second question, you've written a bunch of code I don't think you need. The best way to fix bugs is to eliminate code :-). Again, why are you writing your own authentication views in the first place? – ChrisGPT was on strike Jun 07 '20 at 15:41
  • i am working on somethinng which needs email to login with and thats why i wrote that authentication , acctually i havent wrote i copied. @Chris – utkarsh rai Jun 07 '20 at 15:46
  • That explains the back-end code, but not the `JoinRequest` and `LoginREQUEST` view functions. – ChrisGPT was on strike Jun 07 '20 at 15:47
  • am using ajax to send that data to the function these 2 functions dont have rendering they are just a way to send the data and i am getting the data because i tried to print the variables and they are having the same data i sent from the from through ajaxx – utkarsh rai Jun 07 '20 at 15:50

0 Answers0