1
在home.html中,{{form.as_p}}沒有顯示窗體,無法弄清楚什麼是問題,但一切看起來都正確。ModelForm沒有在Django中顯示
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>
主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