将所选值从Django表单发送到views.py [英] send selected value from Django forms to views.py

查看:87
本文介绍了将所选值从Django表单发送到views.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to send some data to my database where I have a phone object. For this I need to select the desired phone number and insert data to it in the database. A requirement is to display the phone numbers that belongs to the current login user.

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

In my form I have 3 textInputs and one MultipleChoiceField where the different phone numbers are displayed. My question is: how can I get the selected phone_num instance on my view.py and send it to my form ?

我的view.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:

My 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)


推荐答案

您可以简单地访问任何字段值您的表单形式为 clean_data ,如果表单有效:

You can simply access any field value in you form in form cleaned_data, if the form is valid:

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

有关详细信息,请检查< a href =https://docs.djangoproject.com/en/dev/ref/forms/api/#accessing-clean-data =nofollow>访问清理数据

For more details check accessing-clean-data

这篇关于将所选值从Django表单发送到views.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆