Python

Django user and group

Creating Group

from django.contrib.auth.models import Group

group = Group(name="GroupName")
group.save()

-----------------------------------
Adding user in to group

g = Group.objects.get(name='GroupName') 
g.user_set.add(user)

-----------------------------------

get user groups

user.groups.all()

for g in request.user.groups.all():
    l.append(g.name)
-----------------------------------

login required decorator and passes test

from django.contrib.auth.decorators import login_required, user_passes_test

def is_in_multiple_groups(user):
return user.groups.filter(name__in=['hr'])

@login_required(login_url='/login/')
@user_passes_test(is_in_multiple_groups, login_url='/login/')
def dashboard(request):
     #some code


ref:-https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-login-required-decorator


By bm on July 8, 2014 | Python Django | A comment?

Django how to log a user In and Out

from django.contrib.auth import authenticate, login & logout

def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login & logout(request, user)
            # Redirect to a success page.
        else:
            # Return a 'disabled account' error message
    else:
        # Return an 'invalid login & logout' error message.





def logout_view(request):
    logout(request)
   



ref:- https://docs.djangoproject.com/en/1.5/topics/auth/default/#how-to-log-a-user-in

By bm on | Python Django | A comment?

Django how to authenticat User

from django.contrib.auth import authenticate

def some_view(request):
    user = authenticate(username='john', password='secret')
    
    if user is not None:
        # the password verified for the user
        if user.is_active:
            print("User is valid, active and authenticated")
        else:
            print("The password is valid, but the account has been disabled!")
    else:
        # the authentication system was unable to verify the username and password
        print("The username and password were incorrect.")
By bm on | Python Django | A comment?

Django how to change user password

from django.contrib.auth.models import User

def some_view(request):
    u = User.objects.get(username='john')
    u.set_password('new password')
    u.save()

ref :-https://docs.djangoproject.com/en/1.5/topics/auth/default/#changing-passwords

By bm on | Python Django | A comment?

How to create user in Django

from django.contrib.auth.models import User

def some_view(request):
    user = User.objects.create_user('john', '[email protected]', 'johnpassword')
    # if you want to change other fields.
    user.last_name = 'Lennon'
    user.save()

 

ref:-https://docs.djangoproject.com/en/1.5/topics/auth/default/#creating-users

By bm on | Python Django | A comment?

Django how to add app model in admin panel

Open admin.py in app folder and past this code

 

from django.contrib import admin

from myapp.models import Person

admin.site.register(Person)

 

Person :- this is a class defined in models  (myapp/models.py)

By bm on | Python Django | A comment?