>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
# At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = 'Lennon'
>>> user.save()
from django.contrib.auth import get_user_model
User = get_user_model()
def sample_view(request):
current_user = request.user
print current_user.id
from django.conf import settings
from django.shortcuts import redirect
def my_view(request):
if not request.user.is_authenticated:
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
# ...
author = models.ForeignKey(settings.AUTH_USER_MODEL)
if request.user.is_authenticated:
# Do something for authenticated users.
else:
# Do something for anonymous users.
author = models.ForeignKey(settings.AUTH_USER_MODEL)
author = models.ForeignKey(settings.AUTH_USER_MODEL)