I am learning django and I am stuck with this problem.
How do I get the email address of all the users in Django. The user can be of any type like superuser, general user etc.
I tried the following but I am not getting email address of all the users.
user = User.objects.get(id=2)
user_email = user. Email
print(user_email)
I want something like a list of all the email addresses. Can someone please help me with it?
email_list = list(User.objects.values_list("email", flat=True))
Read more about values_list https://docs.djangoproject.com/en/4.1/ref/models/querysets/#values-list
Related
I'M new to Django, looking to get first name and last name of the logged-in user in Django,
login method am using LDAP authentication.
when i login as admin i was able to see the LDAP user in User information tab.
Tried request.user.first_name didn't work, is there any other method to get the details?
You can use this method to get first name, last name and all details of the logged in user.
from django.contrib.auth.models import User
first_name = User.objects.get(username=request.user).first_name
last_name = User.objects.get(username=request.user).last_name
In the same way you can get any attribute of the user. For logged in users, you have to use the keyword request.user.
There is two method in AbstractUser class, you can use this
request.user.get_full_name()
request.user.get_short_name()
get_short_name will return first_name
What is the best way to implement django 2 login and registration like instagram.com. user can register with username and email or phone and password. after register user can login with email address, phone number or username.
Thanks
A great thing about Django is that it comes with a User model already. You just have to apply it to your site.
Check out the user documentation Here
This will give you all the fields you can use, and how to make a member style website
After authentification with social auth plugin, plugin create new user with not beauty usernames like sergey.kostin.345, I know that some users has a nice shorturls on the social media platforms and its ok for default behavior but I want to give user ability to change user names. As far as I understand django auth system does not let me to change User.username field by using methods. I also tried to change this field by using this code, but it seems to be ignoring in django.
owner = User.objects.get (id=request.user.id)
owner.username = newusername
owner.save()
owner is authenticated user
That would work, but there's no need to get the user again. request.user is already the user object.
owner = request.user
owner.username = newusername
owner.save()
In the models.py of django/django/contrib/auth the username is length is changed to 64 fields instead of 30 and the username in database has varchar(64).But it doesnt allow to login with a long username like harry#sdsdasfdfdfgdfgdfgdfgdfgdfg.com
how can this be fixed
class User(models.Model):
"""
Users within the Django authentication system are represented by this model.
Username and password are required. Other fields are optional.
"""
username = models.CharField(_('username'), max_length=64, unique=True, help_text=_("Required. 64 characters or fewer. Letters, numbers and #/./+/-/_ characters"))
I would recommend that you not monkey patch Django, but instead create your own authentication backend. You can then validate against a user's email like in this example: http://djangosnippets.org/snippets/1845/
I know this is an old question, but I feel it's time for a new answer since custom User models is arguably the best feature of django 1.5.
From now on one can create his own custom User model and use it along with existing authentication functionality. The latest documentation covers it all in detail here https://docs.djangoproject.com/en/dev/topics/auth/#auth-custom-user.
Hey buddy there is a nice application named django-registration in the bitbucket, i m sure u will get a possible way to accomplish your task. Please let me know if this link helps you out. http://bitbucket.org/ubernostrum/django-registration
Okay, this one is pretty obvious to everyone who use Django and frequently asked by newbies, but I'd like to make it clear and discuss if there are any other ways to do it. The most widespread and convenient approach now is to store email in username field as Django 1.2 allows "#", "_" and "-" characters, but this way has following issues:
The worst one: username field is restricted by max_length=30 property, which is ridiculously small for emails. Even if you override form validation, DB will have varchar(30) instead of EmailField's varchar(75) unless you alter your table manually.
You need to store your email data both in username and email field to make User.email_user() working. I think there are some other places when User.email is used.
Code readability fail. Sure, other djangonauts know about this pitfall, but treating field called 'username' (especially when there is still email field) as email obviously makes your code less understandable.
The other approach could be authentication using email field by passing it to your auth backend like so, but it still has problems:
authenticate(self, email=None, password=None)
User.email doesn't have unique=True property, which means that your DB won't have index, making your lookups by email slow like hell.
You have to deal with username field, which has unique=True, by completely removing it from your table or altering it to allow NULL and removing index.
Resuming, both ways are evil and require DB-specific code to be executed after syncdb, which is unacceptable if you need DB-independent application.
I've packaged up django-email-as-username which should pretty much do everything you need if you're looking to remove usernames, and only use emails.
The brief overview is:
Provides an email auth backend and helper functions for creating users.
Patches the Django admin to handle email based user authentication.
Overides the createsuperuser command to create users with email only.
Treats email authentication as case-insensitive.
Under the hood usernames are hashed versions of the emails, which ends up meaning we're not limited to the Django's username 30 char limit (Just the regular email 75 char limit.)
Edit: As of Django 1.5, you should look into using a custom User model instead of the 'django-email-as-username' package.
David Cramer came up with a solution to this problem that I love. I'm currently using it on a production site where the user has to be able to log in using their email OR their username. You can find it here:
Logging In With Email Addresses in Django
If the login name provided on the form is an email (contains the '#' symbol), it will attempt to authenticate with that, and will fall back on the username if it isn't an email. (Naturally, you just need to make sure your registration form captures an email for this work.)
Well, I haven't had to use emails as usernames in Django but I guess You could create a UserProfile model and aggregate fields to it, like another email field and make it unique. So you could do user.get_profile().email for your authentication.
I guess other way to go would be to inherit User and redefine the fields, but I think this still not recommended by Django developers.
Finally you could define your own custom User model and back on the django.contrib.auth.models.User for some logic.
Code to alter User table within Django:
from django.db import connection
cursor = connection.cursor()
cursor.execute("ALTER TABLE auth_user MODIFY COLUMN username varchar(75) NOT NULL")