Django表单字段未出现在网页上
我在django表单中添加的字段在网页上不可见。 附上模型、视图和html,供下文参考。 这是一个额外的领域,我打算添加到表格中,我是Django的新手,通过加强当前项目来学习。 “预计员工人数”是我在表格中添加的新字段。 谢谢 型号Django表单字段未出现在网页上,django,forms,Django,Forms,我在django表单中添加的字段在网页上不可见。 附上模型、视图和html,供下文参考。 这是一个额外的领域,我打算添加到表格中,我是Django的新手,通过加强当前项目来学习。 “预计员工人数”是我在表格中添加的新字段。 谢谢 型号 class EstimatedHeadcount(models.Model): count = models.CharField(max_length=100) def __unicode__(self): return self
class EstimatedHeadcount(models.Model):
count = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Meta:
default_permissions = []
@staticmethod
def __gotoadmin__():
return True
<div class="content">
<form id='add_new_client_form' method="post" action="">
{% csrf_token %}
<table class="table">
<tbody>
{{ client_profile_form.as_table }}
</tbody>
<tfoot>
<tr>
<td></td>
<td>
<button class="lock" type="button"
onclick="unlock(this, '#add_new_client_form')">Unlock
</button>
<button type="submit">SAVE</button>
</td>
</tr>
</tfoot>
</table>
</form>
</div>
forms.py
class ClientProfileForm(forms.ModelForm):
class Meta:
model = ClientProfile
fields = ('full_name', 'short_name', 'account_payable',
'require_job_number', 'currency', 'segment', 'market', 'estimated_headcount', 'is_technicolor',
'address')
def client_profile(request):
all_profiles = ClientProfile.objects.filter(status='active')
profile = None
pid = request.GET.get('pid')
client_profile_form = ClientProfileForm()
if pid:
profile = ClientProfile.objects.get(id=pid)
client_profile_form = ClientProfileForm(instance=profile)
if request.method == 'POST':
client_profile_form = ClientProfileForm(request.POST, instance=profile)
if client_profile_form.is_valid():
profile = client_profile_form.save()
profile.csv_mapping = profile.full_name
profile.save()
if profile:
for task_type in TaskType.objects.all():
if not profile.task_costs.filter(task_type=task_type):
task_cost = TaskCost(task_type=task_type)
task_cost.save()
profile.task_costs.add(task_cost)
return render(request, "prod/client_profile.html", {'all_profiles': all_profiles,
'profile': profile,
'client_profile_form': client_profile_form})
视图.py
class ClientProfileForm(forms.ModelForm):
class Meta:
model = ClientProfile
fields = ('full_name', 'short_name', 'account_payable',
'require_job_number', 'currency', 'segment', 'market', 'estimated_headcount', 'is_technicolor',
'address')
def client_profile(request):
all_profiles = ClientProfile.objects.filter(status='active')
profile = None
pid = request.GET.get('pid')
client_profile_form = ClientProfileForm()
if pid:
profile = ClientProfile.objects.get(id=pid)
client_profile_form = ClientProfileForm(instance=profile)
if request.method == 'POST':
client_profile_form = ClientProfileForm(request.POST, instance=profile)
if client_profile_form.is_valid():
profile = client_profile_form.save()
profile.csv_mapping = profile.full_name
profile.save()
if profile:
for task_type in TaskType.objects.all():
if not profile.task_costs.filter(task_type=task_type):
task_cost = TaskCost(task_type=task_type)
task_cost.save()
profile.task_costs.add(task_cost)
return render(request, "prod/client_profile.html", {'all_profiles': all_profiles,
'profile': profile,
'client_profile_form': client_profile_form})
clientprofile.html
class EstimatedHeadcount(models.Model):
count = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Meta:
default_permissions = []
@staticmethod
def __gotoadmin__():
return True
<div class="content">
<form id='add_new_client_form' method="post" action="">
{% csrf_token %}
<table class="table">
<tbody>
{{ client_profile_form.as_table }}
</tbody>
<tfoot>
<tr>
<td></td>
<td>
<button class="lock" type="button"
onclick="unlock(this, '#add_new_client_form')">Unlock
</button>
<button type="submit">SAVE</button>
</td>
</tr>
</tfoot>
</table>
</form>
</div>
{%csrf_令牌%}
{{client_profile_form.as_table}
解锁
拯救
从您的代码中我可以看出,ClientProfile
模型和EstimatedHeadcount
模型之间没有关系
class ClientProfile(models.Model):
...
estimated_headcount = models.CharField(max_length=100)
估计的\u员工人数
应该是客户档案
模型上的一个字段
class ClientProfile(models.Model):
...
estimated_headcount = models.CharField(max_length=100)
旁注:我希望估计的员工人数是一个数值,因此整数字段或正整数字段可能是更好的选择。您能更清楚地解释您的问题吗?哪些字段没有显示,而您希望显示哪些字段?另外,请修复您在上面发布的代码上的缩进。很抱歉,没有提及“估计的\u人数”是我在表单中添加的新字段。
estimate\u headcount
是模型上的字段吗?是的,问题用模型代码更新。不要用双下划线名称定义您自己的方法。您的方法应该只调用gotoadmin()
。(虽然我不知道为什么它是一个方法,因为它只返回一个布尔值;为什么不是一个简单的属性?)谢谢你的回答…现在有一个新的错误