Answers for "how to create user in django"

0

create django user command line

user@hostname$ python3 -m django shell
>>> import django.contrib.auth
>>> User = django.contrib.auth.get_user_model()
>>> user = User.objects.create_user('username', password='userpassword')
>>> user.is_superuser = False
>>> user.is_staff = False
>>> user.save()
Posted by: Guest on February-20-2021
1

how to require login for Django function views

To require login for Django function-based views
  
from django.contrib.auth.decorators import login_required

@login_required
def your_view():
    .....
        
      To require login for Django class-based views
      
from django.contrib.auth.mixins import LoginRequiredMixin

class MyView(LoginRequiredMixin, View):
	.....
Posted by: Guest on February-08-2021
1

django create user

from django.contrib.auth.models import User
user = User.objects.create_user(value['USERNAME'], value['EMAIL'], value['PASSWORD'])
user.first_name = value['NAME']
user.last_name = value['SURNAME']
user.save()
Posted by: Guest on February-11-2021
1

django user permission check

# all your import goes here for Models.py
...

# Adding Permissions to a Model.
class BlogPost(models.Model):
  ... # model fields
  class Meta:
    permissions = [('can_write_blog', 'Can Write Blog')]
    

# Checking permissions in Views.py
if request.user.has_perm('app_name.can_write_blog'):
  # give access to blog post form
else:
  # restrict user from access the page.
Posted by: Guest on October-13-2020
0

Django Custom user model

...
class User(AbstractUser):
    def __unicode__(self):
        return self.username
    balance = models.IntegerField(default=0)
    total_pledged = models.IntegerField(default=0)
    last_pledged = models.ForeignKey('Transaction', related_name='pledger', blank=True, null=True)
    extension_key = models.CharField(max_length=100, null=True, blank=True)
    plugin_key = models.CharField(max_length=100, null=True, blank=True)
    ghosted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        print('saving')
        try:
            self.company.save()
        except:
            print('no company')
        super(User, self).save(*args, **kwargs)
...
Posted by: Guest on May-30-2021
0

how to create staff account in django

user = User.objects.create_user('john', '[email protected]', 'johnpassword')  
user.is_staff=True 
user.save()
Posted by: Guest on September-11-2020

Code answers related to "how to create user in django"

Python Answers by Framework

Browse Popular Code Answers by Language