ModelForm没有在Django模板中显示

时间:2015-08-17 09:46:30

标签: python django django-templates modelform

在home.html中,{{form.as_p}}没有显示表单,无法弄清问题是什么,但一切似乎都正确。

views.py:

class DeptForm(ModelForm):
    class Meta:
        model = Department
        fields = ['dep_name']




def save(request):
    form = DeptForm(request.POST or None)
    return render(request, 'EmpManagement/home.html', {'form':form})   

home.html的:

   <div class="messagepop pop">
    <form action = "{% url 'save' %}" method="post" id="new_message" action="/messages">

    {{form.as_p}}
    <p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
</form>
</div>

main urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^employee/', include('EmployeeManagement.urls')),
)

urls.py(在EmployeeManagement应用程序中):

  urlpatterns = [
      # ex: /polls/
      url(r'^$', views.home, name='home'),
      url(r'^(?P<emp_id>[-\w]+)/$', views.get_employee,             name='get_employee'),
      ur(lr'^(?P<emp_id>[-\w]+)/(?P<status>[-\w]+)/$',        views.change_status, name='change_status'),
      url(r'save$',views.save, name = 'save')
  ]

models.py:

from django.db import models

    class Department(models.Model):
    dep_name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.dep_name

    class Employee(models.Model):
        STATUS = (
        ('Present', 'Present'),
        ('Absent', 'Absent'),
        ('Suspended', 'Suspended'),
        ('OnLeave', 'OnLeave'),
    )

    name = models.CharField(max_length=50)
    age = models.PositiveIntegerField(default=0)
    email = models.CharField(max_length=30)
    address = models.CharField(max_length=100)
    mobile = models.CharField(max_length=12)
    status = models.CharField(max_length=20, choices=STATUS)
    department_id = models.ForeignKey(Department)
    designation = models.CharField(max_length=10)
    salary = models.PositiveIntegerField(default=0)
    hire_date = models.DateField()
    left_date = models.DateField(null=True, blank=True)

    def check_status(self):
        if self.status == 'Present':
            return "text-success"
        elif self.status == 'Absent':
            return "text-danger"
        elif self.status == 'Suspended':
            return "text-info"
        else:
            return "text-warning"
    def __unicode__(self):
        return self.name

    class EmployeeRecords(models.Model):
        CHANGE_TYPE = (
        ('Promoted', 'Promoted'),
        ('Fired', 'Fired'),
        ('Suspended', 'Suspended'),
        ('Resigned', 'Resigned'),
        ('Leaves', 'Leaves'),
        ('ab','ab')
    )

    employee_id = models.ForeignKey(Employee, null=True, blank=True)
    change_type = models.CharField(max_length=2, choices=CHANGE_TYPE,        null=True, blank=True)
    change_date = models.DateField(null=True, blank=True)
    leave_date = models.DateField(null=True, blank=True)

    def __unicode__(self):
        return self.change_type

1 个答案:

答案 0 :(得分:0)

想象一下,在home.html中将{{form.as_p}}更改为{{form}}。 SycnedDB并重新启动服务器。现在工作