0

I'm learning django by myself and as you can imagine it's quite difficult.
I was trying to make a self-adapting form in a view. The app has a model where are stored all the infos about the form (like the placeholder, id and type of the input fields) via JSON (if there's an easier or smarter way I think it'll be better).

all the objects inside the module

an example of an object inside the module

So, in the views file I've passed to the template (picture below) 2 variables which refer to the dict from the object in the picture above.

from django.shortcuts import render
from fattura.models import JobType
import json

def fattura(request):
    datas = JobType.objects.all()
    for i in datas:
        if i.jobType==0:
            dts = json.loads(i.inputs)
            break
    return render(request, 'fattura/fattura.html', {"dts": dts, "inputs": dts["inputs"]})

So, now my scope is to extract the values from the keys "inputType" and "placeholder" in a for statement in the template, in order to insert them into the html file.

<main>
  <div class="form">
    <div class="title>Crea Fattura</div>
    <div class="subtitle">Compila tutti i campi correttamente.</div>
    {% for campo in inputs %}
    <div class="input-container ic2">
      <input id="{{ campo }}" class="input" type="{{ inputs.campo.inputType }}" placeholder=" " />
      <div class="cut"></div>
      <label for="{{ campo }}" class="placeholder">{{ inputs.campo.placeholder }}</label>
    </div>
    {% endfor %}
    <button type="text" class="submit">Invia</button>
  </div>
</main>

With the method above I obviously cannot retrieve anything, but I'm not able to resolve this problem.
This is the view by the way (it is an example, that's why it has only two fields):

enter image description here

  • 2
    Please see https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question – nigel239 Aug 26 '22 at 11:18
  • 1
    Your question seems to be how to iterate over keys + values of a dictionary https://stackoverflow.com/a/3294897/202168 the same will apply in your django template i.e. `{% for key, item in inputs.items %}` and then access fields on that via `{{ item.inputType }}` etc – Anentropic Aug 26 '22 at 11:25
  • 1
    or this answer specifically how to iterate over a dictionary in a django template https://stackoverflow.com/a/8018989/202168 – Anentropic Aug 26 '22 at 11:26
  • 1
    Also in your view function it is inefficient that you query the db for all records and then iterate over them to find the first one where `jobType == 0`... just request the one record you need from the db e.g. `jt = JobType.object.get(jobType=0)` (I assume since the model is `JobType` that `jobType` is the primary key, i.e. there is only one record with `jobType==0`) – Anentropic Aug 26 '22 at 11:29
  • 1
    @nigel239 my bad, I'll use markdown next time. –  Aug 26 '22 at 11:30

1 Answers1

0

The inputs field seems that contains a dictionary; in the template you have

{% for campo in inputs %}

but this is valid for lists, maybe you want to use a nested iterators through dictionaries:

{% for key, value in inputs.items %}
   {% if key == 'inputs' %}
      {% for input_name, input_value in value.items %}
          # {{ input_name }} can be nome, cognome, nascita or ammontare
          # {{ input_value }} is the value
      {% endfor %}
   {% endif %}
{% endfor %}

my advice is to organize better the data inside you json field.

Lorenzo Prodon
  • 343
  • 1
  • 8
  • I don't know why but I actually didn't try to use two iterators . Maybe it's because of the morning. Thanks, as soon as I can I'll tick this as the correct answer. –  Aug 26 '22 at 11:28
  • It's a consequence of how your data are structured. – Lorenzo Prodon Aug 26 '22 at 11:31