为什么我会收到此错误?任何人都可以帮助我...?
why i am getting this error?can anyone help me...?
用户注册使用 mysql django
这是我遇到的错误,无法解决 it.Can 任何人帮助我,我正在使用 python django 和 mysql 作为数据库使用自定义用户模型...?
AttributeError at /accountregister/
'Manager' object has no attribute 'create_user'
Request Method: POST**strong text**
Request URL: http://127.0.0.1:8000/accountregister/
Django Version: 3.2.7
Exception Type: AttributeError
Exception Value:
'Manager' object has no attribute 'create_user'
Exception Location: /home/akshay/Django/mysite/account/views.py, line 20, in register
Python Executable: /home/akshay/Django/my_env/bin/python
Python Version: 3.8.10
Python Path:
['/home/akshay/Django/mysite',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/akshay/Django/my_env/lib/python3.8/site-packages']
Server time: Wed, 16 Feb 2022 06:38:41 +0000
Traceback Switch to copy-and-paste view
/home/akshay/Django/my_env/lib/python3.8/site-packages/django/core/handlers/exception.py, line 47, in inner
response = get_response(request) …
▶ Local vars
/home/akshay/Django/my_env/lib/python3.8/site-packages/django/core/handlers/base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
/home/akshay/Django/mysite/account/views.py, line 20, in register
user=Newuser.objects.create_user(username=username,Email=Email,pwd1=pwd1,pwd2=pwd2,gender=gender) …
▶ Local vars
这是我的views.py
from django.shortcuts import render
from django.contrib.auth.models import *
from .models import *
from django.contrib import messages
def index(request):
return render(request,'accounts/index.html')
def register(request):
if request.method == 'POST':
username = request.POST['uname']
Email = request.POST['email']
pwd1 = request.POST['pass1']
pwd2 = request.POST['pass2']
gender=request.POST['gend']
user=Newuser.objects.create_user(username=username,Email=Email,pwd1=pwd1,pwd2=pwd2,gender=gender)
user.save()
messages.success("user saved successfully....!")
return render(request,'accounts/register.html')
else:
return render(request,'accounts/register.html')
这是我的models.py......................
class Newuser(models.Model):
username=models.CharField(max_length=50)
Email=models.CharField(max_length=50)
pwd=models.CharField(max_length=50)
gender=models.CharField(max_length=1)
这是我的urls.py......................
urlpatterns = [
path('', views.index, name='index'),
path('register/',views.register, name='register')
]
尝试 create
而不是 create_user
:
user=Newuser.objects.create(
username=username,
Email=Email,
pwd=pwd1,
gender=gender)
pwd1=pwd1
和 pwd2=pwd2
在这里不起作用,因为 Newuser 没有名为 pwd1
和 pwd2
.
的字段
另外:关于先学习一些教程的评论可能看起来很苛刻,但它们是对的。您可以从官方的 django 教程开始:
https://docs.djangoproject.com/en/4.0/intro/tutorial01/
或者 YT 上的一些教程。一开始,很多事情会很奇怪,但一段时间后,它就像第二天性一样。
用户注册使用 mysql django
这是我遇到的错误,无法解决 it.Can 任何人帮助我,我正在使用 python django 和 mysql 作为数据库使用自定义用户模型...?
AttributeError at /accountregister/
'Manager' object has no attribute 'create_user'
Request Method: POST**strong text**
Request URL: http://127.0.0.1:8000/accountregister/
Django Version: 3.2.7
Exception Type: AttributeError
Exception Value:
'Manager' object has no attribute 'create_user'
Exception Location: /home/akshay/Django/mysite/account/views.py, line 20, in register
Python Executable: /home/akshay/Django/my_env/bin/python
Python Version: 3.8.10
Python Path:
['/home/akshay/Django/mysite',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/akshay/Django/my_env/lib/python3.8/site-packages']
Server time: Wed, 16 Feb 2022 06:38:41 +0000
Traceback Switch to copy-and-paste view
/home/akshay/Django/my_env/lib/python3.8/site-packages/django/core/handlers/exception.py, line 47, in inner
response = get_response(request) …
▶ Local vars
/home/akshay/Django/my_env/lib/python3.8/site-packages/django/core/handlers/base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
/home/akshay/Django/mysite/account/views.py, line 20, in register
user=Newuser.objects.create_user(username=username,Email=Email,pwd1=pwd1,pwd2=pwd2,gender=gender) …
▶ Local vars
这是我的views.py
from django.shortcuts import render
from django.contrib.auth.models import *
from .models import *
from django.contrib import messages
def index(request):
return render(request,'accounts/index.html')
def register(request):
if request.method == 'POST':
username = request.POST['uname']
Email = request.POST['email']
pwd1 = request.POST['pass1']
pwd2 = request.POST['pass2']
gender=request.POST['gend']
user=Newuser.objects.create_user(username=username,Email=Email,pwd1=pwd1,pwd2=pwd2,gender=gender)
user.save()
messages.success("user saved successfully....!")
return render(request,'accounts/register.html')
else:
return render(request,'accounts/register.html')
这是我的models.py......................
class Newuser(models.Model):
username=models.CharField(max_length=50)
Email=models.CharField(max_length=50)
pwd=models.CharField(max_length=50)
gender=models.CharField(max_length=1)
这是我的urls.py......................
urlpatterns = [
path('', views.index, name='index'),
path('register/',views.register, name='register')
]
尝试 create
而不是 create_user
:
user=Newuser.objects.create(
username=username,
Email=Email,
pwd=pwd1,
gender=gender)
pwd1=pwd1
和 pwd2=pwd2
在这里不起作用,因为 Newuser 没有名为 pwd1
和 pwd2
.
另外:关于先学习一些教程的评论可能看起来很苛刻,但它们是对的。您可以从官方的 django 教程开始: https://docs.djangoproject.com/en/4.0/intro/tutorial01/
或者 YT 上的一些教程。一开始,很多事情会很奇怪,但一段时间后,它就像第二天性一样。