首页 > 解决方案 > DatePicker 使用 ModelForm 不可见 - 脆

问题描述

我有一个基于我的模型随机化的 modelForm 我在我的模板 (Randomzation_edit.html) 和 DateInput() 中使用我的模板 (RandomizationForm)

不幸的是,我的编辑表单中没有 DatePicker

问题是什么?

表格.py

from django import forms
from .models import Randomisation

class RandomisationForm(forms.ModelForm):

    TYPES = [
        (1, 'On-line'),
        (2, 'Telephon'),
    ]
    ran_num = forms.CharField(label="Patient code",disabled=True)
    ran_dat = forms.DateInput()
    ran_pro = forms.ChoiceField(label="Type", widget=forms.Select, choices=TYPES)

    class Meta:
        model = Randomisation
        fields = ('ran_num','ran_dat','ran_inv','ran_pro','ran_pro_per','ran_crf_inc','ran_tbc','ran_crf_eli','ran_cri','ran_sta','ran_vih','ran_bra','ran_med',)

模型.py

class Randomisation(models.Model):

    ran_ide = models.AutoField(primary_key=True)
    ran_num = models.CharField("Patient code", max_length=10, unique=True, null=True, blank=True)
    ran_dat = models.DateTimeField("Today's date", null=True, blank=True)
    ran_inv = models.CharField("Investigator", max_length=20, null=True, blank=True)
    ran_pro = models.IntegerField("Procedure used to randomized", null=True, blank=True)
    ran_pro_per = models.IntegerField("if telephone, name of the person reached on the phone", null=True, blank=True)
    ran_crf_inc = models.IntegerField("Was the CRF inclusion 2 Eligibility fulfilled?", null=True, blank=True)
    ran_tbc = models.IntegerField("Is TB diagnosis possible, probable or definite?", null=True, blank=True)
    ran_crf_eli = models.IntegerField("Was the CRF Inclusion 2 Eligibility fulffiled?", null=True, blank=True)
    ran_cri = models.IntegerField("Is the conclusion All criteria fulfilled for randomisation", null=True, blank=True)
    ran_sta = models.IntegerField("British Medical Council Staging (worst stage reported between first symptoms and now)", null=True, blank=True)
    ran_vih = models.IntegerField("HIV/AIDS status", null=True, blank=True)
    ran_bra = models.IntegerField("TB treatment assigned", null=True, blank=True)
    ran_med = models.IntegerField("Batch number", null=True, blank=True)
    ran_log_dat = models.DateTimeField("Date", null=True, blank=True)
    ran_log = models.CharField("Name of the person who performs randomisation on site", max_length=10, null=True, blank=True)

    class Meta:

        db_table = 'crf_ran'
        verbose_name_plural = 'randomized'
        ordering = ['ran_num']

    def __str__(self):

        return f"{self.ran_ide}"

Randomization_edit.html

{% extends 'layouts/base.html' %}
{% load static %}
{% load crispy_forms_tags %}

{% block title %}Index | Intense TBM{% endblock %}
{% block content %}

<div class='container'>

<h1>Randomization form</h1>
</br>
    <form method="POST" class="post-form">
        {% csrf_token %}
        {{ form |crispy }}
        <button type="submit" class="btn btn-primary">Randomize</button>
    </form>

</div>
</br></br></br></br>
{% endblock %}

标签: pythondjango

解决方案


这里的问题DateTimefield是该字段的默认输入类型是TextInput由于 html 无法将其识别为type=date表单中未显示日期选择器的原因。要将输入类型更改为datetime-local或更改为,date您必须使用 django 中的小部件,这有助于操作特定的 html 输入代码。专门针对这种情况,你可以试试这个,看看它是否有效:

模型.py

class Randomisation(models.Model):

ran_ide = models.AutoField(primary_key=True)
ran_num = models.CharField("Patient code", max_length=10, unique=True, null=True, blank=True)
ran_dat = models.DateField("Today's date", null=True, blank=True)
ran_inv = models.CharField("Investigator", max_length=20, null=True, blank=True)
ran_pro = models.IntegerField("Procedure used to randomized", null=True, blank=True)
ran_pro_per = models.IntegerField("if telephone, name of the person reached on the phone", null=True, blank=True)
ran_crf_inc = models.IntegerField("Was the CRF inclusion 2 Eligibility fulfilled?", null=True, blank=True)
ran_tbc = models.IntegerField("Is TB diagnosis possible, probable or definite?", null=True, blank=True)
ran_crf_eli = models.IntegerField("Was the CRF Inclusion 2 Eligibility fulffiled?", null=True, blank=True)
ran_cri = models.IntegerField("Is the conclusion All criteria fulfilled for randomisation", null=True, blank=True)
ran_sta = models.IntegerField("British Medical Council Staging (worst stage reported between first symptoms and now)", null=True, blank=True)
ran_vih = models.IntegerField("HIV/AIDS status", null=True, blank=True)
ran_bra = models.IntegerField("TB treatment assigned", null=True, blank=True)
ran_med = models.IntegerField("Batch number", null=True, blank=True)
ran_log_dat = models.DateField("Date", null=True, blank=True)
ran_log = models.CharField("Name of the person who performs randomisation on site", max_length=10, null=True, blank=True)

class Meta:

    db_table = 'crf_ran'
    verbose_name_plural = 'randomized'
    ordering = ['ran_num']

def __str__(self):

    return f"{self.ran_ide}"

表格.py

from django import forms
from .models import Randomisation

class RandomisationForm(forms.ModelForm):

TYPES = [
    (1, 'On-line'),
    (2, 'Telephon'),
]
ran_num = forms.CharField(label="Patient code",disabled=True)
ran_dat = forms.DateField(
          widget=forms.TextInput(
        attrs={
            'type': 'date',
            'placeholder' : 'Date',
        }
    )
)
ran_pro = forms.ChoiceField(label="Type", widget=forms.Select, choices=TYPES)

class Meta:
    model = Randomisation
    fields = ('ran_num','ran_dat','ran_inv','ran_pro','ran_pro_per','ran_crf_inc','ran_tbc','ran_crf_eli','ran_cri','ran_sta','ran_vih','ran_bra','ran_med',)

我建议你去看看Django Fields以及Django widgets。我希望这会有所帮助。随时提出进一步的疑问。


推荐阅读