User() 在创建用户时得到了一个意外的关键字参数 'profile_image'
User() got an unexpected keyword argument 'profile_image' when creating user
我使用 from django.contrib.auth.models import User, auth 创建了一个用户注册表单,但是当我尝试提交表单而不是创建新用户时,它抛出了一个错误如:TypeError at /register/
User() 得到了一个意外的关键字参数 'profile_image' ?有人知道我的代码有什么问题吗?
注册模板:
<form action="{% url 'register' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Username</label>
<input type="text" class="form-control" name="username">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">First Name</label>
<input type="text" class="form-control" name="first_name">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Last Name</label>
<input type="text" class="form-control" name="last_name">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Enter Your Email</label>
<input type="email" class="form-control" name="email">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Password</label>
<input type="password" class="form-control" name="password">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Repeat Password</label>
<input type="password" class="form-control" name="password2">
</div>
<br>
<div class="mb-3">
<label for="formFile" class="form-label">Profile Image</label>
<input class="form-control" type="file" id="formFile" name="profile_image">
</div>
<br>
<a href="{% url 'login' %}" style="text-decoration: none; font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Already Have An Account</a>
<br>
<br>
<button type="submit" class="btn btn-warning">Create</button>
</form>
这是我的 profie_image 模板。
<div class="mb-3">
<label for="formFile" class="form-label">Profile Image</label>
<input class="form-control" type="file" id="formFile" name="profile_image">
</div>
注册视图:
def register(request):
if request.method == 'POST':
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
profile_image =request.POST['profile_image']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email or user name Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password, profile_image=profile_image)
user.save();
return redirect('login')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
如我在评论中所说,要解决此问题,您可以创建其他模型
1)models.py
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to="avatars/")
#you can other fields here if you want
def __str__(self):
return self.pk
2)运行 python manage.py makemigrations and python manage.py migrate
3)
def register(request):
if request.method == 'POST':
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
profile_image =request.POST['profile_image']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email or user name Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password)
Profile.objects.create(user=user,profile_image=profile_image)
return redirect('login')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
注意:不要忘记导入 Profile 模型。
5)另一种选择是扩展用户模型你可以参考这个
我使用 from django.contrib.auth.models import User, auth 创建了一个用户注册表单,但是当我尝试提交表单而不是创建新用户时,它抛出了一个错误如:TypeError at /register/ User() 得到了一个意外的关键字参数 'profile_image' ?有人知道我的代码有什么问题吗?
注册模板:
<form action="{% url 'register' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Username</label>
<input type="text" class="form-control" name="username">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">First Name</label>
<input type="text" class="form-control" name="first_name">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Last Name</label>
<input type="text" class="form-control" name="last_name">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Enter Your Email</label>
<input type="email" class="form-control" name="email">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Password</label>
<input type="password" class="form-control" name="password">
</div>
<br>
<div class="form-group">
<label style="font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Repeat Password</label>
<input type="password" class="form-control" name="password2">
</div>
<br>
<div class="mb-3">
<label for="formFile" class="form-label">Profile Image</label>
<input class="form-control" type="file" id="formFile" name="profile_image">
</div>
<br>
<a href="{% url 'login' %}" style="text-decoration: none; font-family: Arial, Helvetica, sans-serif; color: dodgerblue;">Already Have An Account</a>
<br>
<br>
<button type="submit" class="btn btn-warning">Create</button>
</form>
这是我的 profie_image 模板。
<div class="mb-3">
<label for="formFile" class="form-label">Profile Image</label>
<input class="form-control" type="file" id="formFile" name="profile_image">
</div>
注册视图:
def register(request):
if request.method == 'POST':
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
profile_image =request.POST['profile_image']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email or user name Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password, profile_image=profile_image)
user.save();
return redirect('login')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
如我在评论中所说,要解决此问题,您可以创建其他模型
1)models.py
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to="avatars/")
#you can other fields here if you want
def __str__(self):
return self.pk
2)运行 python manage.py makemigrations and python manage.py migrate
3)
def register(request):
if request.method == 'POST':
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
profile_image =request.POST['profile_image']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email or user name Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password)
Profile.objects.create(user=user,profile_image=profile_image)
return redirect('login')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
注意:不要忘记导入 Profile 模型。
5)另一种选择是扩展用户模型你可以参考这个