英文:
Form field not showing in tempate
问题
在定义表单字段后,当我尝试在Django模板中使用{{form.Date_Joined}}访问时
不起作用
class UserAdd(forms.ModelForm):
class Meta:
model=CompanyUsers
fields= '__all__'
exclude=['usrPsswd','usrId','usrCmpnyId','lstLogin','is_active','usrDOJ']
widgets={
'Date_Joined':forms.DateInput(attrs={'type':'date'}),
}
我声明的小部件不显示
<div class="order">
<!--<div class="head" >-->
<h4> ADD COMPANY</h4>
<!--</div> -->
<table id="table-datas">
<form method="POST" action="">
{% csrf_token %}
<tbody>
<tr><td>Username</td><br><td>{{ form.usrNme}}</td></tr>
<tr><td>First Name </td><br><td>{{ form.usrFNme}}</td></tr>
<tr><td>Last Name </td><br><td>{{ form.usrLNme}}</td></tr>
<tr><td>Date of join</td><br><td>{{form.Date_Joined}}</td></tr>
<tr><td>Department</td><br><td>{{ form.usrDpt}}</td></tr>
<tr><td>Email</td><br><td>{{ form.usrMail}}</td></tr>
<tr><td><button type="submit">Register</button></td></tr>
</form>```
所以"Date Joined"字段什么都不显示。
<details>
<summary>英文:</summary>
after defining form field when I try to access in django template using {{form.Date_Joined}}
not working
class UserAdd(forms.ModelForm):
class Meta:
model=CompanyUsers
fields= '__all__'
exclude=['usrPsswd','usrId','usrCmpnyId','lstLogin','is_active','usrDOJ']
widgets={
'Date_Joined':forms.DateInput(attrs={'type':'date'}),
}
The widget i've declare not showing
```<div class="table-data">
<div class="order">
<!-- <div class="head" > -->
<h4> ADD COMPANY</h4>
<!-- </div> -->
<table id="table-datas">
<form method="POST" action="">
{% csrf_token %}
<tbody>
<tr><td>Username</td><br><td>{{ form.usrNme}}</td></tr>
<tr><td>First Name </td><br><td>{{ form.usrFNme}}</td></tr>
<tr><td>Last Name </td><br><td>{{ form.usrLNme}}</td></tr>
<tr><td>Date of join</td><br><td>{{form.Date_Joined}}</td></tr>
<tr><td>Department</td><br><td>{{ form.usrDpt}}</td></tr>
<tr><td>Email</td><br><td>{{ form.usrMail}}</td></tr>
<tr><td><button type="submit">Register</button></td></tr>
</form>```
so the Date Joined field shows nothing
</details>
# 答案1
**得分**: 1
"attrs"的"type"未被考虑,这源自小部件的"input_type",因此您可以创建自定义小部件:
```python
class MyDateInput(forms.DateInput):
input_type = 'date'
然后将其用作小部件插入:
class UserAdd(forms.ModelForm):
class Meta:
model = CompanyUsers
fields = '__all__'
exclude = [
'usrPsswd',
'usrId',
'usrCmpnyId',
'lstLogin',
'is_active',
'usrDOJ',
]
widgets = {
'Date_Joined': MyDateInput,
}
英文:
The type
of the attrs
is not taken into account, this originates from the input_type
of the widget, you thus create a custom widget:
<pre><code>class MyDateInput(forms.DateInput):
<b>input_type = 'date'</b></code></pre>
then we plug this in as widget:
<pre><code>class UserAdd(forms.ModelForm):
class Meta:
model = CompanyUsers
fields = 'all'
exclude = [
'usrPsswd',
'usrId',
'usrCmpnyId',
'lstLogin',
'is_active',
'usrDOJ',
]
widgets = {
'Date_Joined': <b>MyDateInput</b>,
}</code></pre>
答案2
得分: 1
我认为你在表单的**Date_Joined
**字段的拼写上犯了一个错误,因为Python是区分大小写的。
将代码更改为:
widgets={
'date_Joined':forms.DateInput(attrs={'type':'date'}),
}
而不是:
widgets={
'Date_Joined':forms.DateInput(attrs={'type':'date'}),
}
英文:
I think you make a mistake with the typo field of the form Date_Joined
because python is case sensitive.
Type
widgets={
'date_Joined':forms.DateInput(attrs={'type':'date'}),
}
Instead of
widgets={
'Date_Joined':forms.DateInput(attrs={'type':'date'}),
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论