【问题标题】:Django forms field not appearing on webpageDjango 表单字段未出现在网页上
【发布时间】:2017-10-26 05:46:27
【问题描述】:

我在 django 表单中添加的字段在网页上不可见。 附上模型、视图和 html 供下面参考。 这是我打算添加到表单中的一个附加文件,我是 Django 新手,并通过增强当前项目来学习。 “estimated_headcount” 是我在表格中添加的新字段。 谢谢

型号

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

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')

views.py

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

<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>

【问题讨论】:

  • 你能更清楚地解释你的问题是什么吗?哪些字段没有出现您希望出现的?还请修复您在上面发布的代码的缩进。
  • 抱歉没有提及,“estimated_headcount”是我在表格中添加的新字段。
  • estimated_headcount 是模型上的字段吗?
  • 是的,问题已更新为模型代码。
  • 不要使用双下划线名称定义自己的方法。你的方法应该被称为gotoadmin()。 (虽然我不确定为什么它是一个方法,因为它只返回一个布尔值;为什么不是一个简单的属性?)

标签: django forms


【解决方案1】:

据我所知,ClientProfile 模型和 EstimatedHeadcount 模型之间没有关系。

estimated_headcount 应该是 ClientProfile 模型上的一个字段。

class ClientProfile(models.Model):
    ...
    estimated_headcount = models.CharField(max_length=100)

旁注:我希望估计的人数是一个数值,因此 IntegerField 或 PositiveIntegerField 可能是更好的选择。

【讨论】:

  • 感谢您的所有回答...现在有一个新错误
猜你喜欢
  • 2011-03-02
  • 2019-01-08
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
相关资源
最近更新 更多