如何更新 Django 模型用户的first_name、last_name和电子邮件,并更新单独模型中的其他字段



我已经使用YT、StackOverflow、Django文档和其他来源学习Django教程好几个星期了。所以,也许我的问题有点离谱。我希望你能理解我的问题。如果我的代码有任何错误,或者你想给我一些建议,请回复我。

我在电脑上本地制作这个网站。首先,我使用创建了一个注册页面和登录页面Django模型。

用户登录网站后,在配置文件设置中,我创建了表单字段,如first_namelast_name子邮件mobile_number出生日期以及zip_code

我有型号.py用于帐户设置

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Account(models.Model):
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
profile_photo = models.ImageField(upload_to='profile', null=True)
mobile_number = models.PositiveBigIntegerField(null=True)
date_of_birth = models.DateField(auto_now_add=False, auto_now=False, null= True)
zip_code = models.IntegerField(null=True)
address = models.CharField(max_length=225, null=True)
website = models.URLField(max_length=100, null=True)
bio = models.CharField(max_length=225, null=True)

形式.py

from django import forms
from .models import Account
class AccountForm(forms.ModelForm):
class Meta:
model = Account
fields = '__all__'

模板

<form class="form mx-auto my-5" method="POST" action="" enctype="multipart/form-data">
<input name="user" type="hidden" value="{{request.user.id}}" required>
<div class="form-row">
<div class="col form-group">
<label for="first_name" class="font-weight-bold">First name (required):</label>
<input name="first_name" type="text" id="first_name" class="form-control" placeholder="First name" value="{% if request.user.first_name %}{{request.user.first_name}}{% endif %}" required>
</div>
<!-- <div class="col form-group">
<label for="middle_name">Middle name (optional):</label>
<input name="middle_name" type="text" id="middle_name" class="form-control" placeholder="Middle name">
</div> -->
<div class="col form-group">
<label for="last_name">Last name (optional):</label>
<input name="last_name" type="text" id="last_name" class="form-control" placeholder="Last name" value="{% if request.user.last_name %}{{request.user.last_name}}{% endif %}">
</div>
</div>
<div class="form-group">
<label for="email" class="font-weight-bold">Email address:</label>
<input name="email" type="email" id="email" class="form-control" value="{{request.user.email}}" placeholder="Your email" required>
</div>
<div class="form-row">
<div class="col form-group">
<label for="mobile_number" class="font-weight-bold">Mobile number:</label>
<input name="mobile_number" type="tel" id="mobile_number" class="form-control" placeholder="98********" required>
</div>
<div class="col form-group">
<label for="date_of_birth" class="font-weight-bold">Date of birth:</label>
<input name="date_of_birth" type="date" id="date_of_birth" class="form-control" required>
</div>
<div class="col form-group">
<label for="zip_code" class="font-weight-bold">Zip code:</label>
<input class="form-control" id="zip_code" name="zip_code" type="number" min="00000" max="99999" placeholder="55455">
</div>
</div>
<div class="form-row">
<div class="col form-group">
<label for="address">Address:</label>
<input name="address" type="text" id="address" class="form-control" placeholder="Bharatpur-07, Chitwan, Nepal">
</div>
</div>
<div class="form-row">
<div class="col form-group">
<label for="website">Website:</label>
<input name="website" type="url" id="website" class="form-control" placeholder="https://example.com/">
</div>
</div>
<div class="form-row">
<div class="col form-group">
<label for="bio">Bio:</label>
<textarea name="bio" class="form-control" id="bio" rows="5"></textarea>   
</div>
<div class="col form-group">
<br><br>
<span>Write something about your</span>
</div>
</div>
<button class="btn btn-primary btn-md px-3 my-0 mr-0 float-right" type="submit">Submit</button
</form>

我在视图中尝试过。py,成功了。

from django.shortcuts import render, redirect
from .forms import AccountForm
from django.contrib import messages
from django.contrib.auth.models import User
# Create your views here.
def profileSettingsPage(request, pid):
form = AccountForm()
if request.method == 'POST':
form = AccountForm(request.POST, request.FILES)
if int(request.POST.get('user')) == request.user.id:
if form.is_valid():
user = User.objects.get(pk=pid)
user.first_name = request.POST.get('first_name')
user.last_name = request.POST.get('last_name')
user.email = request.POST.get('email')
user.save()
form.save()
messages.success(request, 'Profile saved.')
return redirect('profile-settings', pid)
else:
messages.warning(request, 'Sorry! You enter other username.')
return redirect('profile-settings', pid)
context = {'form':form}
return render(request, 'profile-settings.html', context) 

但是,我想在保存前验证的first_name、last_name和电子邮件字段。我希望保存一些is_valid((

此外,我尝试了一些新的

现在,在视图.py中,我添加了

from pages.forms import CreateUserForm

它是为注册页面创建的

然后,views.py看起来是这样的:

from django.shortcuts import render, redirect
from .forms import AccountForm
from pages.forms import CreateUserForm
from django.contrib import messages
from django.contrib.auth.models import User
# Create your views here.
def profileSettingsPage(request, pid):
form1 = AccountForm()
form2 = CreateUserForm()
if request.method == 'POST':
form1 = AccountForm(request.POST, request.FILES)
form2 = CreateUserForm(request.POST)
if int(request.POST.get('user')) == request.user.id:
if form1.is_valid() and form2.is_valid():
form2.save()
form1.save()
messages.success(request, 'Profile saved.')
return redirect('profile-settings', pid)
else:
messages.warning(request, 'Faile to saved profile')
return redirect('profile-settings', pid)
else:
messages.warning(request, 'Sorry! You enter other username.')
return redirect('profile-settings', pid)
context = {'form1':form1, 'form2':form2}
return render(request, 'profile-settings.html', context)

但是,这对我来说不起作用。form1.is_valid((form2.is_vvalid(([/strong>无法传递True值并给我消息";未能保存配置文件">

我的问题是,我想更新Django模型User的first_namelast_name子邮件字段,其他字段分别更新到下一个模型。

另一个问题

在上面的models.py中,我使用ForeignKey((来获取用户(用户名或id(。

模板中,我使用{{request.user.id}}来获取用户。

是否可以在不接触视图.py、表单.py模板的情况下获取用户(id或用户名(

编辑:我在这个链接上得到了答案:我无法在Django Views.py 中同时更新两个模型

您可以简单地将上下文中的user_id传递给模板,然后使用它。

class Account(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.SET_NULL)
profile_photo = models.ImageField(upload_to='profile', null=True)
mobile_number = models.PositiveBigIntegerField(null=True)
date_of_birth = models.DateField(auto_now_add=False, auto_now=False, null= 
True)
zip_code = models.IntegerField(null=True)
address = models.CharField(max_length=225, null=True)
website = models.URLField(max_length=100, null=True)
bio = models.CharField(max_length=225, null=True)
def profileSettingsPage(request):
form1 = AccountForm()
form2 = CreateUserForm()
if request.method == 'POST':
instance_user = User.objects.get(pk=request.user.id)
instance_account = Account.objects.get(user=instance_user)
form1 = AccountForm(request.POST, request.FILES,instance=instance_account)
form2 = CreateUserForm(request.POST,instance=instance_user)
if form1.is_valid() and form2.is_valid():
form2.save()
form1.save()
messages.success(request, 'Profile saved.')
return redirect('profile-settings')
else:
print(form1.errors)
print(form2.errors)
messages.warning(request, 'Faile to saved profile')
return redirect('profile-settings')
context = {'form1':form1, 'form2':form2}
return render(request, 'profile-settings.html', context)

相关内容

  • 没有找到相关文章

最新更新