简体   繁体   English

单击提交按钮时,下拉列表未在Django表单的DB中填充

[英]Drop down lists not populating in DB in Django Forms on click of submit button

I have a table inside form. 我在表格内有一张桌子。 It looks like below: 如下图所示:

{% extends "base.html" %}

{% block title %}Title{% endblock title %}
{% block content %}
<form actions="" method="post">
{% csrf_token %}
<table>
    <table border = "1" cellpadding = "10" cellspacing = "10" bordercolor = "green">
    <tr>
        <th>numbers</th>
        <th>Extension</th>
        <th>Vendor</th>
    </tr>
        {% for number in numbers %}
    <tr>
        <td>{{ number }}</td>
        <td class = "select">Select Extension
        <select name="extensions">
            {% for obj in sipextensionsets %}
            <option value={{obj.sip_extension}}>{{ obj.sip_extension }}</option>
            {%  endfor %}
        </select>
        </td>

        <td>vendor</td>
    </tr>
        {% endfor %}
</table>

<input type="submit" value="save"/>
</form>
{% endblock content %}

My forms.py is below: 我的forms.py如下:

from django import forms

from .models import column

class didsForm(forms.ModelForm):

    class Meta:
        model = column
        fields = ('extension')

My views.py is below 我的views.py在下面

def saveintodb(request):
    try:
       instance = coloumn.objects.get(pk=1)
    except:
        instance = coloumn(pk=1)
        instance.save()
    if request.method == 'POST':
        dids_form = didsForm(data=request.POST['extensions'], instance=instance)
        if dids_form.is_valid():
            dids_form.save()
            messages.success(request, "Settings updated. Please apply settings.")
        else:
            messages.error(request, "Error: Invalid settings.")
    else:
        dids_form = didsForm(instance=instance)

    return render(request, 'dids/index.html', {'dids_form': dids_form})

In the table, there is a drop down (select tag). 在表格中,有一个下拉列表(选择标记)。 I want to save the data into database when user selects something from dropdown and clicks on save button. 当用户从下拉列表中选择某项并单击“保存”按钮时,我想将数据保存到数据库中。 I know I have mistaken somewhere in views. 我知道我在视图中的某个地方弄错了。

You're doing a few things wrong here, unfortunately. 不幸的是,您在这里做错了几件事。

The main problem is that you're passing request.POST['extensions'] as the data argument to your form on POST; 主要问题是您正在将request.POST['extensions']作为data参数传递给POST上的表单; but that argument is expecting the whole POST, not a single field. 但是该参数期望整个POST,而不是单个字段。

Linked to that is that you have not used the same name for the field in the model and the field in the form. 链接到的是您没有为模型中的字段和表单中的字段使用相同的名称。 Although you say in your comment that this is intentional, there doesn't seem to be a reason for it, and it's breaking things. 尽管您在评论中说这是故意的,但似乎没有任何理由,这正在破坏事情。 Give them the same name. 给他们起同样的名字。

Thirdly, you aren't letting Django populate the form, or show any errors when it's not valid. 第三,您不要让Django填充表格,或者在无效时显示任何错误。 You shouldn't be explicitly passing sipextenionset (although you actually don't seem to be passing that at all, so I'm not sure where it's coming from), and you certainly shouldn't be explicitly iterating. 您不应该显式地传递sipextenionset (尽管您实际上似乎根本没有传递sipextenionset ,所以我不确定它的来源),并且您当然也不应显式地进行迭代。 You should let Django display the field: 您应该让Django显示字段:

   <td>{{ number }}</td>
   <td class="select"><label for="id_extension">Select Extension</label>
   {{ form.extension }}
    </td>

Finally, I can't at all understand what you are doing with that outer for loop through numbers ; 最后,我完全无法理解您正在使用外部for循环遍历numbers you will end up with several values for extension , which is not expected by your form, your model, or your view. 您最终将获得几个extension值,这是表单,模型或视图所不希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM