【问题标题】:send selected value from Django forms to views.py将选定的值从 Django 表单发送到 views.py
【发布时间】:2015-12-21 08:07:49
【问题描述】:

我必须将一些数据发送到我有电话对象的数据库。为此,我需要选择所需的电话号码并将数据插入数据库中。要求是显示属于当前登录用户的电话号码。

在我的表单中,我有 3 个 textInputs 和一个 MultipleChoiceField,其中显示了不同的电话号码。我的问题是:如何在 view.py 上获取选定的 phone_num 实例并将其发送到我的表单?

我的观点.py:

def phone_config(request):
    phone = Phone.objects.get(phone_num = 611111111)
    phone_nums = Phone.objects.filter(user_id = request.user.id).values_list('phone_num', flat=True)

    if request.method == 'POST':
        form = phoneForm(phone_nums, request.POST, instance=phone)
        if form.is_valid():
            form.save()
            return redirect(reverse('gracias'))
    else:
        form = phoneForm(phone_nums, instance=phone)
    return render(request, 'heroconfigurer/heroconfigurer.html', {'form': form})


def gracias_view(request):
    return render(request, 'heroconfigurer/gracias.html')

我的forms.py:

class phoneForm(ModelForm):

    class Meta:
        model = Phone
        fields = ['phone_num', 'num_calls', 'time_btwn_calls', 'psap']
        widgets = {'phone_num': Select(attrs={'class': 'form-control'}), 
                'num_calls': TextInput(attrs={'class': 'form-control'}),
                'time_btwn_calls': TextInput(attrs={'class': 'form-control'}), 
                'psap': TextInput(attrs={'class': 'form-control'})
                  }
        labels = {
                'phone_num': ('Select phone number'),
                'num_calls': ('Number of calls'),
                'time_btwn_calls': ('Time between calls'),
                'psap': ('PSAP'),
        }

    def __init__(self, phone_nums, *args, **kwargs):
        super(tcuForm, self).__init__(*args, **kwargs)
        self.fields['phone_num'].queryset = Sim.objects.filter(phone_num__in = phone_nums)

【问题讨论】:

    标签: python django django-models django-forms


    【解决方案1】:

    如果表单有效,您可以在表单 cleaned_data 中简单地访问表单中的任何字段值:

    view.py:

    def phone_config(request):
        phone_nums = Phone.objects.filter(user_id = request.user.id).values_list('phone_num', flat=True)
    
        if request.method == 'POST':
            form = phoneForm(request.POST)
            if form.is_valid():
                phone_num = form.cleaned_data.get('phone_num') # here you get the selected phone_num
                phone_obj = Phone.objects.get(phone_num=phone_num)
    
            ... # the rest of the view
    

    更多详情请查看accessing-clean-data

    【讨论】:

    • 我需要实例化选定的电话号码以更新数据库中的数据存储
    • phone_obj = Phone.objects.get(phone_num=phone_num) 返回手机的实例。 phone_obj 是实例。
    • 我不能在分配之前引用'phone_num'
    • 你是怎么做到的?如果您执行 `phone_num = form.cleaned_data.get('phone_num') ` then `phone_obj = Phone.objects.get(phone_num=phone_num)` 你不会得到这样的错误,只有当你引用另一个变量时放在 if 语句之外:form.is_valid().
    • 系统不知道要更新数据的phone_num,因此它会在数据库中创建一个新的phone对象,用于更新所选phone_num上的数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2019-03-13
    • 2019-11-24
    相关资源
    最近更新 更多