Z
Z
zhanka_sun2018-08-06 16:25:59
PostgreSQL
zhanka_sun, 2018-08-06 16:25:59

Why is the data from the checkbox not saved in the database?

Hello! There is a form where you can edit the database data. When I change the data from the text fields, the data in the database also changes, but when I select items from the checkbox, the old data disappears in the database, but the new ones are also not saved, but simply filled with null.
models:

class test_dic2(models.Model):
    word2 = models.TextField(max_length = 100)
    emo2 = models.CharField(max_length = 100, choices=Emotion1)
    ton2 = models.CharField(max_length = 100, choices=Status1)
    rus2 = models.TextField(max_length = 100)
    eng2 = models.TextField(max_length = 100)
    syn2 = models.TextField(max_length = 100, null = True)
    slang = models.TextField(max_length = 100, null = True)
    cat_pol = models.BooleanField("pol", blank=True)
    cat_eco = models.BooleanField("eco",  blank=True)
    cat_med = models.BooleanField("med",  blank=True)
    cat_cul = models.BooleanField("cul", blank=True)
    cat_sport = models.BooleanField("sport",  blank=True)
    cat_all = models.BooleanField("all",  blank=True)
    date = models.DateTimeField(auto_now=True)

view:
def dic_edit(request, id):
    try: 
        d_edit = test_dic2.objects.get(id=id)
 
        if request.method == "POST":
            d_edit.word2 = request.POST.get("word2")
            d_edit.emo2 = request.POST.get("emo2")
            d_edit.ton2 = request.POST.get("ton2")
            d_edit.rus2 = request.POST.get("rus2")
            d_edit.eng2 = request.POST.get("eng2")
            d_edit.syn2 = request.POST.get("syn2")
            d_edit.cat_pol = request.POST.get("cat_pol")
            d_edit.cat_eco = request.POST.get("cat_eco")
            d_edit.cat_med = request.POST.get("cat_med")
            d_edit.cat_cul = request.POST.get("cat_cul")
            d_edit.cat_sport = request.POST.get("cat_sport")
            d_edit.cat_all = request.POST.get("cat_all")

            d_edit.save()
            return HttpResponseRedirect("/dictionary.html")
        else: 
            return render(request, 'app/dic_edit.html', {
                "d_edit": d_edit
                })
    except dic_kaz.DoesNotExist:
        return HttpResponseNotFound("<h2>Theme not found</h2>")

html:
<form method="post">
            <div>
                <table width="80%">
                                <tr>
                                    <td >Слово:</td>
                                    <td><input type="text" name="word2" size="50" value="{{d_edit.word2}}"/></td>
                                </tr>
                                <tr>
                                    <td>Эмоция:</td>
                                    <td>
                                        <select name="emo2" required_id ="{{d_edit.emo2}}" >
                                            <option value="{{d_edit.emo2}}" selected="{{d_edit.emo2}}">{{d_edit.emo2}}</option>   
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Тональность:</td>
                                    <td>
                                        <select name="ton2" required_id ="{{d_edit.ton2}}" >
                                                <option value="{{d_edit.ton2}}" selected="{{d_edit.ton2}}">{{d_edit.ton2}}</option>
                                            </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Синонимы:</td>
                                    <td><input type="text" name="syn2" size="50" value="{{d_edit.syn2}}"  /></td>
                                </tr>
                                <tr>
                                    <td>Сленги:</td>
                                    <td><input type="text" name="slang" size="50" value="{{d_edit.slang}}"/></td>
                                </tr>
                                <tr>
                                    <td>На русском:</td>
                                    <td><input type="text" name="rus2" size="50" value="{{d_edit.rus2}}"/></td>
                                </tr>
                                <tr>
                                    <td>На английском:</td>
                                    <td><input type="text" name="eng2" size="50" value="{{d_edit.eng2}}"/></td>
                                </tr>
                                <tr>
                                    <td>Категория</td>
                                    <td> <input  id="cat_pol" type="checkbox" value="{{d_edit.cat_pol}}" /> Политика </td>
                                            <input id="cat_eco" type="checkbox" value="{{d_edit.cat_eco}}" /> Экономика </td>
                                            <input id="cat_cul" type="checkbox" value="{{d_edit.cat_cul}}" /> Культура </td>
                                            <input id="cat_med" type="checkbox" value="{{d_edit.cat_med}}" /> Медицина </td>
                                           <td> <input id="cat_sport" type="checkbox" value="{{d_edit.cat_sport}}" /> Спорт </td>
                                           <td> <input id="cat_all" type="checkbox" value="{{d_edit.cat_all}}" /> Общее </td>
                                </tr>
                </table>
                <p>
                <button type="submit">Сохранить</button>
                <input type="button" onclick="history.back()" value="Отмена">
                </p>
            </div>
        </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2018-08-06
@melkij

TLDR
input without a name is not passed as a form element by the browser. Because it doesn't have a name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question