Issue
I am following a tutorial by Telusko. He switched to PostgreSQL but I decided to continue with SQLite. He showed this code to add a user to the database:
def register(request):
if request.method == "POST":
firstname= request.POST['firstname']
lastname= request.POST['lastname']
email= request.POST['email']
phone= request.POST['phone']
username= request.POST['username']
password1= request.POST['password1']
password2= request.POST['password2']
dob= request.POST['dob']
gender= request.POST['gender']
user = User.objects.create_user(username=username, email=email, phone=phone, password=password1, dob=dob, gender=gender, firstname=firstname, lastname=lastname,)
user.save();
print('user created')
return redirect("/")
else:
return render(request, 'register.html') ;
But I get this error when I submit :
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/register
Django Version: 4.1.3
Python Version: 3.11.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login.apps.LoginConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\yuri\Desktop\naisuback\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\yuri\Desktop\naisuback\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\yuri\Desktop\naisuback\login\views.py", line 30, in register
user = User.objects.create_user(username=username, email=email, phone=phone, password=password1, dob=dob, gender=gender, firstname=firstname, lastname=lastname,)
Exception Type: AttributeError at /register
Exception Value: 'Manager' object has no attribute 'create_user'
I wanted I could enter information into my HTML form (which is configured properly) and it's sent to database using POST method, adding a new user. My database db.sqlite3 is opened automatically when I run server.
I have a table login_user
which already has 2 users I added through the admin panel.
Solution
You must be using django's default ModelManager
, which doesn't have the create_user method. You need to inherit from AbstractBaseUser
class instead of models.Model in your User
model and add objects = UserManager()
to your model, like below:
from django.contrib.auth.models import AbstractBaseUser, UserManager
class User(AbstractBaseUser):
...
objects = UserManager()
Answered By - Divya Prakash
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.