Python Django:request.POST中的值正确,但无法保存表单
在我的注册页面中,我有两个表单UserForm和Profile表单 配置文件表单依赖于配置文件模型,该模型有一个指向用户模型的OneToOneField—Django中的默认用户模型 其思想是,创建用户时,也会创建该用户的配置文件:Python Django:request.POST中的值正确,但无法保存表单,python,django,Python,Django,在我的注册页面中,我有两个表单UserForm和Profile表单 配置文件表单依赖于配置文件模型,该模型有一个指向用户模型的OneToOneField—Django中的默认用户模型 其思想是,创建用户时,也会创建该用户的配置文件: @receiver(post_save, sender=User) def update_user_profile(sender, instance, created, **kwargs): if created: Profile.objec
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
配置文件模型:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# birth_date = models.DateField(null=True, blank=True)
dni = models.CharField(max_length=30, blank=True)
#phone_number = models.CharField(max_length=15, blank=True)
shipping_address1 = models.CharField(max_length=100, blank=False)
shipping_address2 = models.CharField(max_length=100, blank=False)
shipping_department = models.CharField(max_length=100, blank=False)
shipping_province = models.CharField(max_length=100, blank=False)
shipping_district = models.CharField(max_length=100, blank=False)
def __str__(self):
return str(self.user.first_name) + "'s profile"
else:
print("INVALID User_FORM")
print(user_form.errors)
print("INVALID Profile_FORM")
print(profile_form.errors)
INVALID User_Form
INVALID Profile_Form
<ul class="errorlist"><li>shipping_province<ul class="errorlist"><li>Select a valid choice. Tarata is not one of the available choices.</li></ul></li><li>shipping_district<ul
class="errorlist"><li>Select a valid choice. Estique is not one of the available choices.</li></ul></li></ul>
[25/Jan/2019 18:18:38] "POST /account/create/ HTTP/1.1" 200 16522
[25/Jan/2019 18:18:38] "GET /static/css/footer.css HTTP/1.1" 404 1767
问题:UserForm.is\u valid()为True
,但Profile\u Form.is\u valid()为False
但在我的请求中,我得到了ProfileForm的所有正确值:
<QueryDict: {'csrfmiddlewaretoken': ['91sJC01vyty3Z9ECjrSop1bouUOF2ZeC9m6XZUgk0nTEH7p6W0DmtuQaB4EBhV22'], 'first_name': ['Juana'], 'last_name': ['Juana'], 'username': ['juana
juana'], 'dni': ['454545'], 'email': ['juana@gmail.com'], 'password1': ['caballo123'], 'password2': ['caballo123'], 'shipping_address1': ['Urb. La Merced Mz.G Lot.32'], 'ship
ping_address2': ['Villa FAP - San Roque'], 'shipping_department': ['Cajamarca'], 'shipping_province': ['Cajamarca'], 'shipping_district': ['Cajamarca']}>
@transaction.atomic
def signupView(request):
peru = Peru.objects.all()
department_list = set()
province_list = set()
district_list = set()
for p in peru:
department_list.add(p.departamento)
department_list = list(department_list)
print("Department List: ", department_list)
if len(department_list):
province_list = set(Peru.objects.filter(departamento=department_list[0]).values_list("provincia", flat=True))
print("Provice List: ", province_list)
province_list = list(province_list)
# print("dfsfs", province_list)
else:
province_list = set()
if len(province_list):
district_list = set(
Peru.objects.filter(departamento=department_list[0], provincia=province_list[0]).values_list("distrito",
flat=True))
print("district List: ", district_list)
else:
district_list = set()
if request.method == 'POST':
user_form = SignUpForm(request.POST)
profile_form = ProfileForm(district_list, province_list, department_list, request.POST)
print("This is Profile Form: ", profile_form)
print("Is Profile_Form valid: ", profile_form.is_valid())
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
username = user_form.cleaned_data.get('username')
signup_user = User.objects.get(username=username)
customer_group = Group.objects.get(name='Clientes')
customer_group.user_set.add(signup_user)
raw_password = user_form.cleaned_data.get('password1')
user.refresh_from_db() # This will load the Profile created by the Signal
# print(user_form.cleaned_data)
print("Form is valid: district_list, province_list, department_list")
print(district_list, province_list, department_list)
profile_form = ProfileForm(district_list, province_list, department_list, request.POST,
instance=user.profile) # Reload the profile form with the profile instance
profile_form.full_clean() # Manually clean the form this time. It is implicitly called by "is_valid()" method
# print(profile_form.cleaned_data)
profile_form.save() # Gracefully save the form
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('cart:cart_detail')
else:
print("INVALID FORM")
user_form = SignUpForm()
profile_form = ProfileForm(district_list, province_list, department_list)
print('end of profile postdata print')
print(profile_form)
print("Profile Data:")
return render(request, 'accounts/signup.html', {
'user_form': user_form,
'profile_form': profile_form
})
此行返回false:
print("Is Profile_Form valid: ", profile_form.is_valid())
档案格式:
class ProfileForm(ModelForm):
def __init__(self, district_list, province_list, department_list, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
self.fields['shipping_district'] = forms.ChoiceField(choices=tuple([(name, name) for name in district_list]))
self.fields['shipping_province'] = forms.ChoiceField(choices=tuple([(name, name) for name in province_list]))
self.fields['shipping_department'] = forms.ChoiceField(choices=tuple([(name, name) for name in department_list]))
dni = forms.CharField(label='DNI', max_length=100, required=True)
shipping_address1 = forms.CharField(label='Dirección de envío', max_length=100, required=True)
shipping_address2 = forms.CharField(label='Dirección de envío 2 (opcional)', max_length=100, required=False)
class Meta:
model = Profile
fields = ('dni', 'shipping_address1',
'shipping_address2', 'shipping_department', 'shipping_province', 'shipping_district')
更新1:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# birth_date = models.DateField(null=True, blank=True)
dni = models.CharField(max_length=30, blank=True)
#phone_number = models.CharField(max_length=15, blank=True)
shipping_address1 = models.CharField(max_length=100, blank=False)
shipping_address2 = models.CharField(max_length=100, blank=False)
shipping_department = models.CharField(max_length=100, blank=False)
shipping_province = models.CharField(max_length=100, blank=False)
shipping_district = models.CharField(max_length=100, blank=False)
def __str__(self):
return str(self.user.first_name) + "'s profile"
else:
print("INVALID User_FORM")
print(user_form.errors)
print("INVALID Profile_FORM")
print(profile_form.errors)
INVALID User_Form
INVALID Profile_Form
<ul class="errorlist"><li>shipping_province<ul class="errorlist"><li>Select a valid choice. Tarata is not one of the available choices.</li></ul></li><li>shipping_district<ul
class="errorlist"><li>Select a valid choice. Estique is not one of the available choices.</li></ul></li></ul>
[25/Jan/2019 18:18:38] "POST /account/create/ HTTP/1.1" 200 16522
[25/Jan/2019 18:18:38] "GET /static/css/footer.css HTTP/1.1" 404 1767
其他:
打印(“无效用户表单”)
打印(用户表单错误)
打印(“无效的配置文件表单”)
打印(配置文件格式错误)
无效的用户表单
无效的配置文件表单
请选择一个有效的选项。塔拉塔不是可用的选择之一。航运区- 选择一个有效的选项。Estique不是可用的选项之一。
[25/Jan/2019 18:18:38]“POST/account/create/HTTP/1.1”200 16522
[25/Jan/2019 18:18:38]“GET/static/css/footer.css HTTP/1.1”404 1767
表单中的选项字段不需要值列表。您将有多个选项,但该字段只有一个值
那么,试试这个:
profile_form = ProfileForm(request.POST)
并计算装运区
、装运省
和装运部门
的值,作为表单中初始
的一部分
更新:
如果要查看错误,请更改代码:
user_form_valid = user_form.is_valid()
profile_form_valid = profile_form.is_valid()
if user_form_valid and profile_form_valid:
# ...
else:
# print(user_form.errors)
# print(profile_form.errors)
# ...
更新#2
您呈现给用户的表单中的选项与您在视图中绑定的表单中的选项不同,尝试初始化尝试找到一种方法来初始化这些选项,为您实例化的每个ProfileForm
设置相同的值
另一方面,我鼓励您在这种情况下使用。使用:
profile\u form=ProfileForm(request.POST)
返回\uu init\uuuuuu()缺少两个必需的位置参数:'province\u list'和'department\u list'
我说的是在构造函数中设置这些字段的选项,而不是值。是的,我理解。但我如何通过航运区
,航运省
和航运部
作为邮政的一部分?我需要放入ProfileForm(shipping\u deparment=request.POST.get('shipping\u deparment')、shipping\u province=request.POST.get('shipping\u province')、shipping\u district=request.POST.get('shipping\u district')、request.POST)中
?表单的值是什么?表单之后的错误是什么?
是否有效?
无法访问该部分,因为:如果用户表单有效()和配置文件表单有效():
由于配置文件表单无效,我无法传递该条件。请查看我的代码中的更多详细信息,并告诉我应该在哪里调用form.errors
。