Z
Z
zhanka_sun2018-08-07 08:14:22
Django
zhanka_sun, 2018-08-07 08:14:22

Why is the data from the database not displayed on the form?

Hello! There is a database that consists of words, I want to add the ability to edit words. When editing, the data from the text fields is displayed on the form, but the data from the checkbox disappears (that is, those that are True are not checked). And when I save the changes, then in the database, too, all the fields of the checkbox are filled with null.
Models:

class test_dic2(models.Model):
    cat_pol = models.BooleanField("pol", default=False, blank=True)
    cat_eco = models.BooleanField("eco", default=False, blank=True)
    cat_med = models.BooleanField("med", default=False, blank=True)
    cat_cul = models.BooleanField("cul", default=False, blank=True)
    cat_sport = models.BooleanField("sport", default=False, blank=True)
    cat_all = models.BooleanField("all", default=False, blank=True)
    date = models.DateTimeField(auto_now=True)

views:
def dic_edit(request, id):
    try: 
        d_edit = test_dic2.objects.get(id=id)
        if request.method == "POST":
            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 test_dic2.DoesNotExist:
        return HttpResponseNotFound("<h2>Theme not found</h2>")

HTML
<tr>
       <td>Категория</td>
      <td> <input  name="cat_pol" type="checkbox" value="{{d_edit.cat_pol}}" /> Политика 
              <input name="cat_eco" type="checkbox" value="{{d_edit.cat_eco}}" /> Экономика 
              <input name="cat_cul" type="checkbox" value="{{d_edit.cat_cul}}" /> Культура 
              <input name="cat_med" type="checkbox" value="{{d_edit.cat_med}}" /> Медицина 
             <input name="cat_sport" type="checkbox" value="{{d_edit.cat_sport}}" /> Спорт 
             <input name="cat_all" type="checkbox" value="{{d_edit.cat_all}}" /> Общее 
</td>
     </tr>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2018-08-07
@zhanka_sun

<input name="cat_all" type="checkbox" {% if d_edit.cat_all %} checked="checked"{% endif%}" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question