Home > Enterprise >  how to call get_foo_display in html page
how to call get_foo_display in html page

Time:10-06

I want to show in ListView human redable format instead of actual values. Like

From this

| fc_mo | c_serv |
| ---   | ---    |
| 1     | 5      |

To this

| fc_mo | c_serv     |
| ---   | ---        |
| 1     | cardiology |

Or to check it values

I tried to call it in several ways, but it always get no data from it. It only worked in a shell. Please can someone show me example of using get_FOO_display().There is very little information in the django documentation about this

model.py

class LicAppNew(models.Model):
  id_prof = models.AutoField(primary_key=True, serialize=True)
  fc_mo = models.CharField(max_length=8, choices=FC_MO_CHOICES)
  name = models.CharField(max_length=280, blank=True, null=True, choices=FC_NAME_CHOICES_2)
  date_b = models.DateField(blank=True, null=True, default='01.04.2022')
  idpr = models.IntegerField(blank=True, null=True, choices=IDPR_CHOICES)
  disp = models.CharField(max_length=10, blank=True, null=True)
  date_e = models.DateField(blank=True, null=True, default='01.01.2070')
  lic_num = models.CharField(max_length=255, blank=True, null=True)
  c_serv = models.IntegerField(blank=True, null=True, choices=C_SERV_CHOICES)
  deleted = models.BooleanField(default=False, blank=True, null=True)

class Meta:
    managed = True
    db_table = 'lic_app_new'
    #ordering = ['fc_mo']
    ordering = ['fc_mo','c_serv']


#def __str__(self):
   # return self.name()


def get_absolute_url(self):
    return f'/app/{self.id_prof}'

views.py

class LicAppNew_view(ListView):
  model = LicAppNew
  template_name = 'app/LicView_view.html'
  queryset = LicAppNew.objects.all()

class AppLicUpdateView(UpdateView):
    model = LicAppNew
    template_name = 'app/detail_app_view.html'
    form_class = LicAppNewForm
    success_url = '/app/'

LicView_view.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="{% static 'ksg/css/ksg.css' %}">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/fontawesome.min.css">
    </head>
    <body>
        <aside>
            <img src="{% static 'ksg/img/TFOMS_logo.jpg' %}" alt="Лого">
            <h3>Навигация</h3>
            <ul>
                <a href="{% url 'ksg' %}"><li>КСГ</li></a>
                <a href="{% url 'App_view' %}"><li>App</li></a>
                <a href="{% url 'SP_view' %}"><li>SP</li></a>
                <a href="{% url 'SZP_view' %}"><li>SZP</li></a>
                <a href="{% url 'SZP1_view' %}"><li>SZP1</li></a>
            </ul>
        </aside>
      <main>
            <div style="height:100vh;overflow-y:scroll;">
                <table >
            <thead>
                <tr>
                    <th>Номер МО {{ value }}</th>
                    <th>Название МО</th>
                    <th>Дата начала действия лицензии</th>
                    <th>Федеральный Профиль</th>
                    <th>Диспан</th>
                    <th>Приказ</th>
                    <th>Код Специальности(профиля)(Наш) </th>
                    <th>Кнопка добавления новой лицензии </th>
                    <th>Кнопка измения этой лицензии</th>
                    <th>Удаление лицензии</th>
                </tr>
            </thead>
            <tbody>
          {% for form in object_list %}
           {% if form.deleted == False or form.deleted == null %}
                <tr>
                  <!--   <td>{{ serv.id }}</td>-->
                   <!-- <td>{{ serv.id_mo }}</td> -->
                    <td>{{ form.fc_mo }}</td>
                    <td>{{ form.name }}</td>
                    <td>{{ form.date_b }}</td>
                    <td>{{ form.idpr}}</td>
                    <td>{{ form.disp}}</td>
                    <td>{{ form.lic_num}}</td>
                    <td>{{ display_lic }}</td>
                    <td><a href="{% url 'Appcreate' %}"><button >Добавить запись</button></a></td>
                    <td><a href="{% url 'App_update' form.id_prof %}" >Изменить</a></td>
                    <td><a href="{% url 'App_delete' form.id_prof %}" >Удалить</a></td>
                </tr>
           {% endif %}
            {% endfor %}
            </tbody>
<!-- ><i > </tbody>-->
            </table>
            </div>
        </main>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    </body>
</html>

detail_app_view.html

{% load static%}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="{% static 'ksg/css/ksg.css' %}">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/fontawesome.min.css">
</head>
<body>
<div >
<form method="post" >
    <h1>Форма изменения записи</h1>
    {% csrf_token %}<br>
    <p>Номер МО</p>
    <p>{{ form.fc_mo }}</p>
    <p>Название МО</p>
    <p>{{ form.name }}</p>
    <p>Дата начала действия лицензии</p>
    <p>{{ form.date_b }}</p>
    <p>Федеральный профиль</p>
    <p>{{ form.idpr}}</p>
    <p>Номер диспанцеризации</p>
    <p>{{ form.disp}}</p>
    <p>Номер приказа</p>
    <p>{{ form.lic_num}}</p>
    <p>Профиль кода специальности</p>
    <p>{{ form.c_serv}}</p>
    <p>asad{{ form.get_c_serv_display() }}</p>
    <button  type="submit">Изменить</button>
</form>
</div>
</body>
</html>

form.py

class LicAppNewForm(ModelForm):

    class Meta:
        model = LicAppNew
        fields = ['id_prof', 'fc_mo', 'name', 'date_b', 'idpr','disp', 'date_e', 'lic_num', 'c_serv', 'deleted']
        widgets = {
            "date_b": DateInput(attrs={
                'class': 'from-control',
                'placeholder': 'Дата начала действия лицензии'
            }),
            "disp": TextInput(attrs={
                'class': 'from-control',
                'placeholder': 'Диспан'
            }),
            "date_e": DateInput(attrs={
                'class': 'from-control',
                'placeholder': 'Дата конца действия лицензии'
            }),
            "lic_num": TextInput(attrs={
                'class': 'from-control',
                'placeholder': 'Приказ'
            }),
            "deleted": CheckboxInput(attrs={
                'class': 'from-control',
                'placeholder': 'Вы уверены?'
            })
        }
        relevance = forms.ChoiceField(choices = C_SERV_CHOICES)

CodePudding user response:

get_FOO_display is a django model instance "magic" method, not a form attribute. Also, Django template language invokes functions which don't have arguments, but you don't call them with brackets. So not {{form.get_c_serv_display()}}

You possibly want {{form.instance.get_c_serv_display}} if form is a ModelForm. Otherwise you will have to pass instance explicitly in the context, if it's not already there (It probably is there, as the default object of LicAppView, so {{object.get_c_serv_display}} )

  • Related