Post.get Label Text in views.py

Hello everyone,

I have a simple question but I got stuck. How can I get label text in my views.py?

For example, below code prints None instead of the text in label. I set label value by javascript($(“#label_is_edit”).text(is_index_sample_text);) Thanks in advance.

In .html file

<div class="form-check form-switch" style="padding-left: 20%; padding-top: 3%;">
  <input class="form-check-input" style="transform: scale(1.5);" name="is_edit" id="is_edit"  type="checkbox">
  <label class="custom-control-label font-italic" id="label_is_edit" for="is_edit" style="padding-left: 5%;"></label>
</div>

In views.py
print(request.POST.get('label_is_edit'))

You can’t grab the label text from the rendered HTML on the server side. You can fetch the value in JavaScript and include it in the post via hidden input or something else. However, you shouldn’t have to. Typically the label’s text is something that you define when coding the application so your backend should already know about it.

If you explain what your overall goal is, we can likely help more. But for your specific question, no, you can’t get the label’s text in a form submission. Only inputs and textareas.

1 Like