模型.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
phone_daytime = models.CharField(max_length=50, null=True, blank=True)
class User(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=75)
password = models.CharField(max_length=75)
视图.py
def profile(request):
""""""
registerform = UserRegisterForm(request.POST)
createprofileform = UserCreateProfileForm(request.POST)
if registerform.is_valid() and createprofileform.is_valid():
result = registerform.save(commit=False)
result.set_password(request.POST['password'])
result.save()
member.user_id = user.id
member.member_id = result.id
member.save()
member_profile = UserProfile.objects.get(user=result.id)
createprofileform = UserCreateProfileForm(request.POST, instance=member_profile)
createprofileform.save()
return render(request,'profile.html',{'createprofileform':createprofileform,'registerform':registerform})
查看将数据保存到数据库。
模板
<tr>
<td>First name:</td><td>{{registerform.first_name}}</td>
</tr>
<tr>
<td>Last name:</td><td>{{registerform.last_name}}</td>
</tr>
<tr>
<td>Daytime phone:</td><td>{{createprofile.phone_daytime}} </td>
</tr>
<tr>
<td>Email:</td><td>{{registerform.email}} </td>
</tr>
<tr>
<td>Password</td><td>{{registerform.password}} </td>
</tr>
保存的姓名和电子邮件 ID 显示在模板中,如下所示,
<td> <input type="submit" name="delete" value="{{list.0.id}}" class="delete_reporter" /></span>
<a href="{{list.0.id}}">{{list.0.first_name|title}} {{list.0.last_name}}</a></td>
<td> {{list.0.email}} {{list.1.phone_daytime}} {{list.1.phone_mobile}}</td>
在模板中,如果我单击<a href="{{list.0.id}}">{{list.0.first_name|title}} {{list.0.last_name}}</a>
表单应该以可编辑模式打开,这样如果我想进行更改,我可以更改并再次保存它。如何在 django 中执行此操作。我在<a>
标签中传递 id。