Django表示没有出现在网页上的字段

时间:2017-10-26 05:46:28

标签: django forms

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

2 个答案:

答案 0 :(得分:0)

据我所知,您的代码与ClientProfile模型和EstimatedHeadcount模型之间没有任何关系。

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

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

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

答案 1 :(得分:0)

'EstimatedHeadcount' object has no attribute 'name'
Request Method: GET
Request URL:    https://localhost.mumbai.tracevfx.com/prod/client_profile/
Django Version: 1.7.11
Exception Type: AttributeError
Exception Value:    
'EstimatedHeadcount' object has no attribute 'name'
Exception Location: ./base/models.py in __unicode__, line 1311
Python Executable:  /opt/venv/trace_one/bin/uwsgi
Python Version: 2.7.14
Python Path:    
['/opt/venv/trace_one/lib/python2.7/site-packages/',
 '.',
 '',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
 '/opt/repos/trace_one',
 '/opt/repos/trace_one',
 '/opt/cgru/afanasy/python',
 '/opt/cgru/lib/python',
 '/opt/repos/trace_one',
 '/opt/repos/trace_one']
Server time:    Thu, 26 Oct 2017 12:50:30 +0530
Error during template rendering

In template /opt/repos/trace_one/templates/prod/client_profile.html, error at line 107