'ForwardManyToOneDescriptor'对象没有属性'user_type'



Models.py

class UserType(models.Model):
owner=models.ForeignKey(User,on_delete=models.CASCADE,default=1)
user_type=models.CharField(max_length=20,default='admin')

Views.py

def register(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
fname = request.POST.get('firstname')
lname = request.POST.get('lastname')
email = request.POST.get('email')
newpass = make_password(password)
newuser = User.objects.create(username=username,password=newpass,first_name=fname,last_name=lname,email=email)
newuser.save()
usertype = request.POST.get('usertype')
newtype = UserType.objects.create(user_type=usertype)
newtype.save()
messages.success(request, "Registration successful")
if UserType.owner.user_type == 'Student':
return redirect('Stuproadd')
elif UserType.owner.user_type == 'Teacher':
return redirect('Teachproadd')
else:
return redirect('index')
return render(request, 'register.html')

register.html

<body>
<h1>Register</h1>
<div class="container">
<form method="post">
{% csrf_token %}
<label>Username:</label> 
<input type="text" name="username" placeholder="Enter your username" required /><br><br>
<label>First Name:</label>
<input type="text" name="firstname" placeholder="Enter First Name" required /><br><br>
<label>Last Name:</label>
<input type="text" name="lastname" placeholder="Enter last name" /><br><br>
<label>Email:</label>
<input type="text" name="email" placeholder="Enter your email" required /><br><br>
<label>password</label> 
<input type="password" name="password" placeholder="enter your password" required /> <br><br>
<label>confirm password</label> 
<input type="password" name="password" placeholder="confirm your password" required /> <br><br>
<label>Select your Role:</label>
<input type="radio" name="usertype" value="Student">
<label>Student</label>
<input type="radio" name="usertype" value="Teacher">
<label>Teacher</label>
<button type="submit">Register</button>
Already User,<a href="{% url 'index' %}">Login Here</a>
</form></div>
</body>

这就是代码。由于"ForwardManyToOneDescriptor"对象没有属性"user_type",我收到错误

点击注册按钮后,当用户类型为学生时,我想进入stubroadd,当用户类别为教师时,我希望进入teachproad。怎么样?请帮助

您应该使用UserType对象(在您的情况下是newtype)而不是UserType类。

def register(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
fname = request.POST.get('firstname')
lname = request.POST.get('lastname')
email = request.POST.get('email')
newpass = make_password(password)
newuser = User.objects.create(username=username,password=newpass,first_name=fname,last_name=lname,email=email)
newuser.save()
usertype = request.POST.get('usertype')
newtype = UserType.objects.create(user_type=usertype)
newtype.save()
messages.success(request, "Registration successful")
if newtype.owner.user_type == 'Student':  # Use `newtype` instead of `UserType`
return redirect('Stuproadd')
elif newtype.owner.user_type == 'Teacher':  # Use `newtype` instead of `UserType`
return redirect('Teachproadd')
else:
return redirect('index')
return render(request, 'register.html')

相关内容

  • 没有找到相关文章

最新更新