python - 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 %}
解决方案
这里的问题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。我希望这会有所帮助。随时提出进一步的疑问。
推荐阅读
- javascript - 如何更改打字稿中的 CSS 类?
- google-apps-script - 如何修复“异常:在对象 SpreadsheetApp 上打开方法或属性时出现意外错误”?
- mysql - 如果我尝试通过 MySQL 存储过程从表单字段中插入超过 673 个字符的文本,文本就会消失
- javascript - 如何在不使用 JQuery 的情况下使用 web api 和 Javascript 使用 asp.net 执行 2 个数字的加法
- javascript - 为什么我的动画背景没有占据整个屏幕?
- ffmpeg - 使用 ffmpeg 在视频上显示帧速率
- javascript - 如何通过选择一个选项来更改所有输入值
- r - 使用特征多项式根在 ggplot2 中绘制单位圆
- reactjs - 反应切换不改变布尔值
- c++ - 引用字典值以减少查找