user()有一个意外的关键字参数' profile_image'创建用户时
我使用django.contrib.auth.models导入用户,auth 的创建了一个用户注册表单,但是当我尝试提交表单而不是创建新用户时,它会给我带来一个错误,例如: typeError at /nopers / 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')
i created a user registration form using from django.contrib.auth.models import User, auth but when i try to submit the form instead of creating a new user it's throws me an error like: TypeError at /register/
User() got an unexpected keyword argument 'profile_image' ? is anybody who know what's wrong with my code please ?
the register template:
<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>
this is my profie_image template.
<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>
the register view:
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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要解决这个问题,正如我在评论中所说的那样,您可以创建其他模型
1)模型
2)运行
python manage.py makemigrations和python manage.py迁移
3)
nb:不要忘记导入配置文件模型。
5)另一个选择是扩展用户模型,您可以参考此 https://stackover.com/a/a/67204210/1504210/15042684 < /a>
To solve this problem as i said in the comment you can just create an other model
1)models.py
2)run
python manage.py makemigrations and python manage.py migrate
3)
NB:DO not forget to import the Profile model.
5) An other option is to extend the User model you can refer to this https://stackoverflow.com/a/67204210/15042684