Django表单无法保存到表

时间:2019-01-26 20:10:23

标签: python django django-models django-forms django-views

这是问题所在-用户按下“保存”按钮时,应将他们输入的设置保存到表格中。但是,这不会发生。重定向后,您可以再次检查“设置”页面或管理站点,更改不会出现。我什至放了几条全部运行的打印语句,但我不知道为什么会这样。这是代码(我只包含了必要的内容)

views.py

class SettingsView(LoginRequiredMixin, FormView):
template_name = 'example/settings.html'
form_class = ExampleSettingsForm
success_url = reverse_lazy('example:index')

def get_context_data(self, **kwargs):
    context = super(SettingsView, self).get_context_data(**kwargs)
    context['page'] = 'settings'
    return context

def get_initial(self):
    initial = super(SettingsView, self).get_initial()

    initial['example1'] = self.request.user.example.example1
    initial['example2'] = self.request.user.example.example2
    initial['example3'] = self.request.user.example.example3
    initial['example4'] = self.request.user.example.example4
    initial['example5'] = self.request.user.example.example5
    initial['example6'] = self.request.user.example.example6
    initial['example7'] = self.request.user.example.example7

    return initial

def form_valid(self, form):
    form.save_to_example(self.request.user)
    print("Form valid.")
    return super(SettingsView, self).form_valid(form)

@method_decorator(shelter_login_required)
def dispatch(self, *args, **kwargs):
    return super(SettingsView, self).dispatch(*args, **kwargs)

forms.py

class ExampleSettingsForm(forms.Form):
example1 = forms.CharField()

example2 = forms.TimeField()
example3 = forms.TimeField()

example4 = AddressField()

example5 = forms.IntegerField()

example6 = forms.RegexField(regex=r'^\+?1?\d{9,15}$')

example7 = forms.BooleanField()

def save_to_example(self, user):
    user.example.example1 = self.cleaned_data['example1']
    user.example.example2 = self.cleaned_data['example2']
    user.example.example3 = self.cleaned_data['example3']
    user.example.example4 = self.cleaned_data['example4']
    user.example.example5 = self.cleaned_data['example5']
    user.example.example6 = self.cleaned_data['example6']
    user.example.example7 = self.cleaned_data['example7']
    user.save()
    print(user.example.example1)

models.py

class Example(models.Model):
    example1 = models.CharField(max_length=200)

    example2 = models.TimeField(null=True)
    example3 = models.TimeField(null=True)

    example_male = models.IntegerField(default=0)
    example_female = models.IntegerField(default=0)

    example6 = models.IntegerField(default=10)

    example5 = AddressField(null=True, on_delete=models.CASCADE)

    user = models.OneToOneField(User, on_delete=models.CASCADE)

    example7 = models.BooleanField(default=False)

    example_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    example_number = models.CharField(validators=[phone_regex], max_length=17, blank=True)

@receiver(post_save, sender=User)
def create_user_example(sender, instance, created, **kwarg):
    if created:
        Example.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_example(sender, instance, **kwargs):
    if not instance.is_superuser:
        instance.example.save()

注意-我知道缩进不正确。我在复制/粘贴到堆栈溢出时遇到了麻烦。

1 个答案:

答案 0 :(得分:0)

尝试一下:

def save_to_example(self, user):
    example = user.example
    example.example1 = self.cleaned_data['example1']
    example.example2 = self.cleaned_data['example2']
    example.example3 = self.cleaned_data['example3']
    example.example4 = self.cleaned_data['example4']
    example.example5 = self.cleaned_data['example5']
    example.example6 = self.cleaned_data['example6']
    example.example7 = self.cleaned_data['example7']
    example.save()
    print(example.example1)