我有一个典型的场景,我想创建一个注册表单,任何人都可以插入名字,姓氏,电子邮件ID,用户名,密码(可以存储在Django提供的用户表中),个人简介和个人资料图片(不能存储在Django提供的User表中)
注册后,当用户尝试使用用户名和密码登录时,他可以查看其详细信息并进行更新。
我已经通过与用户表的OneToOne关系完成了注册部分,但是查看和更新它们时遇到了很多麻烦,这对Django代码有帮助。
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
work = models.CharField(max_length=120,null=True)
bio = models.TextField(null=True)
profile_picture = models.ImageField(upload_to="Profile Pictures",null=True)
def __str__(self):
return self.user.username
<form action="" method="POST">
{% csrf_token %}
<div class="modal-body">
<div class="md-form form-sm mb-5">
<i class="fas fa-user prefix fb-text "></i>
<input type="text" name="username" class="form-control form-control-sm validate" required>
<label data-error="wrong" data-success="right" for="username">Your Username</label>
</div>
<div class="md-form form-sm mb-5">
<i class="fas fa-envelope prefix fb-text"></i>
<input type="email" name="email" class="form-control form-control-sm validate" required>
<label data-error="wrong" data-success="right" for="email">Your email</label>
</div>
<div class="md-form form-sm mb-5">
<i class="fas fa-envelope prefix fb-text"></i>
<input type="text" name="work" class="form-control form-control-sm validate" required>
<label data-error="wrong" data-success="right" for="work">Your Work</label>
</div>
<div class="md-form form-sm mb-5">
<i class="fas fa-lock prefix fb-text"></i>
<input type="password" name="password1" class="form-control form-control-sm validate" minlength="8" required>
<label data-error="wrong" data-success="right" for="password1" >Your password</label>
</div>
<div class="md-form form-sm mb-4">
<i class="fas fa-lock prefix fb-text"></i>
<input type="password" name="password2" class="form-control form-control-sm validate" minlength="8" required>
<label data-error="wrong" data-success="right" for="password2">Repeat password</label>
</div>
<div class="text-center form-sm mt-2">
<button type="submit" class="btn btn-fb-text">Sign up <i class="ml-1 fas fa-sign-in-alt "></i></button>
</div>
</div>
</form>
def login(request):
if request.method=="GET":
if request.user.is_authenticated:
return redirect("/")
else:
return render(request,"login.html")
else:
if "email" in request.POST:
username=request.POST["username"]
email = request.POST["email"]
work = request.POST["work"]
password1 = request.POST["password1"]
password2 = request.POST["password2"]
if password1 != password2:
messages.error(request,"Passwords Doesn't Match",extra_tags="bg-danger")
elif User.objects.filter(username=username).exists():
messages.info(request,"Username Already Taken",extra_tags="bg-info")
elif User.objects.filter(email=email).exists():
messages.error(request,"Already Registered",extra_tags="bg-danger")
else:
user = User.objects.create_user(username=username,email=email,password=password1)
user.save()
profile = UserProfile()
profile.work= work
profile.user= user
profile.save()
messages.success(request,"Registered Successfully",extra_tags="bg-success")
return redirect("/login")
else:
username=request.POST["username"]
password = request.POST["password"]
user = auth.authenticate(username=username,password=password)
if user is not None:
messages.success(request,"Login Successfully",extra_tags="bg-success")
auth.login(request,user)
return redirect("/")
else:
messages.error(request,"Invalid Credentials",extra_tags="bg-danger")
return redirect("/login")