简体   繁体   中英

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:

from django import forms

from .models import column

class didsForm(forms.ModelForm):

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

My views.py is below

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; but that argument is expecting the whole POST, not a single field.

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. 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. You should let Django display the field:

   <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 ; you will end up with several values for extension , which is not expected by your form, your model, or your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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