10 examples of 'django create user' in Python

Every line of 'django create user' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
24def create_user(**kwargs):
25 global user_number
26
27 password = kwargs.pop("password", None)
28 if "username" not in kwargs:
29 kwargs["username"] = "test%s" % user_number
30 user_number += 1
31
32 from django.contrib.auth.models import User
33
34 user = User(**kwargs)
35 if password:
36 user.set_password(password)
37 user.save()
38 return user
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

20def create_user(self, username='bouke@example.com', password='secret', **kwargs):
21 user = self.User.objects.create_user(username=username, email=username, password=password, **kwargs)
22 self._passwords[user] = password
23 return user
339def create_user(
340 username, email, password, is_staff=False, is_superuser=False, base_cms_permissions=False, permissions=None,
341):
342 from django.contrib.auth.models import Permission
343
344 User = get_user_model() # NOQA
345
346 try:
347 if User.USERNAME_FIELD == "email":
348 user = User.objects.get(**{User.USERNAME_FIELD: email})
349 else:
350 user = User.objects.get(**{User.USERNAME_FIELD: username})
351 except User.DoesNotExist:
352 user = User()
353
354 if User.USERNAME_FIELD != "email":
355 setattr(user, User.USERNAME_FIELD, username)
356
357 try:
358 user.email = email
359 except AttributeError:
360 pass
361 user.set_password(password)
362 if is_superuser:
363 user.is_superuser = True
364 if is_superuser or is_staff:
365 user.is_staff = True
366 user.is_active = True
367 user.save()
368 if user.is_staff and not is_superuser and base_cms_permissions:
369 user.user_permissions.add(Permission.objects.get(codename="add_text"))
370 user.user_permissions.add(Permission.objects.get(codename="delete_text"))
371 user.user_permissions.add(Permission.objects.get(codename="change_text"))
372 user.user_permissions.add(Permission.objects.get(codename="publish_page"))
373
374 user.user_permissions.add(Permission.objects.get(codename="add_page"))
375 user.user_permissions.add(Permission.objects.get(codename="change_page"))
376 user.user_permissions.add(Permission.objects.get(codename="delete_page"))
377 if is_staff and not is_superuser and permissions:
378 for permission in permissions:
379 user.user_permissions.add(Permission.objects.get(codename=permission))
380 return user
214def create_user(**kwargs):
215 "Create an user along with her dependencies"
216 user = UserFactory.create(**kwargs)
217 password = kwargs.pop('password', None)
218 if password:
219 user.set_password(password)
220 user.is_active = True
221 user.save()
222 return user
26def make_user(user_dict):
27 if 'id' in user_dict:
28 user = models.User.load(user_dict['id'])
29 else:
30 name, email = user_dict['nr_name'], user_dict['nr_email']
31 try:
32 user = models.User.create_unregistered(fullname=name,
33 email=email)
34 user.save()
35 except ValidationValueError:
36 user = auth.get_user(email=email)
37 if user is None:
38 logger.error('Could not load user {0}'.format(user_dict))
39 return user
178def pre_create_google_user(self, email, **extra_fields):
179 """ Pre-create a User object for a user who will later log in via Google Accounts. """
180 values = dict(
181 # defaults which can be overriden
182 is_active=True,
183 )
184 values.update(**extra_fields)
185 values.update(
186 # things which cannot be overridden
187 email=self.normalize_email(email),
188 username=None,
189 password=make_password(None), # unusable password
190 # Stupidly, last_login is not nullable, so we can't set it to None.
191 )
192 return self.create(**values)
69def create_user(self, username, **kwargs):
70 """Saves a new user in the datastore for the currently logged in user,
71 and returns it. If the username already exists, returns ``None``.
72
73 :param username:
74 The unique username for this user.
75 :param kwargs:
76 Extra keyword arguments accepted by
77 :class:`tipfy.ext.user.models.User`.
78 :return:
79 The new :class:`tipfy.ext.user.models.User` entity, or ``None`` if
80 the username already exists.
81 """
82 kwargs = {
83 'email': users.get_current_user().email(),
84 'is_admin': users.is_current_user_admin(),
85 }
86 return super(GoogleAuth, self).create_user(username, **kwargs)
62def create_user_(self):
63 user = User(
64 username = self.username,
65 email = self.details['email']
66 )
67 if 'first_name' in self.data:
68 user.first_name = self.data['first_name']
69 if 'last_name' in self.data:
70 user.last_name = self.data['last_name']
71 user.set_password(random_password())
72 user.save()
73 self.user = user
46def create_user(self, email, first_name, last_name, facebook_uid, facebook_access_token, **extra_fields):
47 """
48 Create and save an User with the given email, facebook_uid and name.
49
50 :param email: string
51 :param first_name: string
52 :param last_name: string
53 :param facebook_uid: string
54 :param facebook_access_token: string
55 :param extra_fields:
56 :return: User
57 """
58
59 return self._create_user(email, facebook_uid, facebook_access_token, first_name, last_name, password=None,
60 is_staff=False, is_superuser=False, **extra_fields)
151def _create_user(self, site, kwargs):
152 email, password = kwargs['email'], kwargs['password1']
153 new_user = RegistrationProfile.objects.create_inactive_user(email, email, password, site, send_email=False)
154 new_user.first_name = kwargs['full_name']
155 group = Group.objects.filter(name="NGO Admins")
156 new_user.groups.add(group[0])
157 return new_user

Related snippets