user10784511
user10784511

Reputation:

The user is none and getting logged in django

I am creating a creating a signup and login where i am signing up and saving the credentials in the database and logging with the same credentials. Eventhough it is printing the username and password while authenticating it is saying user as none.

Views.py
--------
def Login(request):
    form = login_form(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data.get("username")
        password = form.cleaned_data.get("password1")
        print (username,password)
        user = authenticate(request,username=username, password=password)
        print('user is', user)
        if user is not None and user.is_active:
            print ('entered loop')
            login(request,user)
            return redirect('/home/')
        else:
            print ("username and password are incorrect ")
    else:
        form = login_form()
    return render(request, 'Login.html', {'form': form})



settings.py:
---------------
AUTHENTICATION_BACKENDS = ("django.contrib.auth.backends.ModelBackend",
                           'django.contrib.auth.backends.RemoteUserBackend',)
AUTH_USER_MODEL = 'auth.User'

urls.py:
---------
urlpatterns = [
    url('admin/', admin.site.urls),
    url('App/', include('django.contrib.auth.urls')),
    url(r'^signup/', views.signup,name='signup'),
    url(r'^Login/', views.Login,name='Login'),
    url(r'^Logout/', views.Logout,name='Logout'),
models.py:
-----------
class MyUserManager(BaseUserManager):
     def create_user(self, fname,lname,username, password):
          """
          Creates and saves a User with the given username, date of
          birth and password.
          """
          if not username:
               raise ValueError('Users must have an username')
          user = self.model(username=username,fname=fname,lname=lname)
          user.set_password(password)
          user.is_active = True
          user.save(using=self._db)
          print (user)
          return user


     def create_superuser(self, fname,lname,username, password,email=None):
          """
          Creates and saves a superuser with the given username and password.
          """
          user = self.create_user(
               fname=fname,
               lname=lname,
               username=username,
               password=password,
          )
          user.is_admin = True
          user.is_superuser = True
          user.save(using=self._db)
          return user

class Event(AbstractBaseUser):
     fname = models.CharField('fname', max_length=120)
     lname = models.CharField('lname',max_length=120)
     username = models.CharField('username',max_length = 60,unique=True)
     password = models.CharField('password',max_length=120,default='pavi@2789')

     USERNAME_FIELD = 'username'
     REQUIRED_FIELDS = ['fname','lname']

     objects = MyUserManager()

     def __unicode__(self):
          return self.username

     class Meta:
          # managed = False
          db_table = "user"

Output:

uname xxxxxx@2789 user is None

User is coming as none eventhough it is printing the username and password.but when i checked in the dadtabase the user is there.I dont know what is going wrong here.

Upvotes: 0

Views: 376

Answers (1)

IVNSTN
IVNSTN

Reputation: 9299

AUTH_USER_MODEL = 'auth.User' whilst you've obviously created your user with Event model.

If you want to keep users in your own table Event then modify AUTH_USER_MODEL to tell django where your users are:

AUTH_USER_MODEL = '<appname>.Event'

Upvotes: 1

Related Questions