Django:更正request.POST中的值,但无法保存表单

时间:2019-01-25 21:36:47

标签: python django

在我的“注册”页面中,我有2个表单用户表单和个人资料表单。

个人资料表单依赖于个人资料模型,该模型具有一个OneToOneField到User模型-Django-中的默认User模型。

这个想法是,当创建用户时,还会创建该用户的配置文件:

@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" 

问题::UserForm.is_valid()为True,但Profile_Form.is_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']}>

如何正确保存ProfileForm?

显示UserForm和ProfileForm的视图:

@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:

    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

1 个答案:

答案 0 :(得分:1)

表单中的选择字段不期望值列表。您将有多个选项,但是该字段将只有一个值。

因此,请尝试以下操作:

 profile_form = ProfileForm(request.POST)

并计算表单中shipping_district的{​​{1}},shipping_provinceshipping_department的值。

更新:

如果要查看错误,请更改您的代码:

__init__

更新#2

您在呈现给用户的表单中的选择与视图中绑定的是不同的,尝试初始化尝试找到一种方法来为每个{{1 }}实例化。

另一方面,我建议您在这种情况下使用FormSet