将多个模型(mysql)组合到Djang中的模板

2022-08-10 12:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有引导设计来绑定我的django crud-like操作。它具有插入数据的模式和在同一页上显示该数据的表。在insert model中有2,3个下拉列表,我想用mysql db记录填充它们。这就是为什么我想在一个def中组合两个情态动词

视图.py

def deptlist(request):
department = Department.objects.all()
return render(request, 'manageemp.html', {'department': department})


def managemp(request):
employees = Employee.objects.all()
return render(request, "manageemp.html", {'employees': employees})

表单.py

class EmpForm(ModelForm):
   class Meta:
     model = Employee
       fields = ["employee_id", "Name", "designation", "department_id", "manager_id", 
        "date_of_joining","date_of_birth", "location_id", "email","contact_number", 
        "password", "created_by", "modified_by", "status", "user_type"]


class dept(ModelForm):
    class Meta:
      model = Department
        fields = ["department_id", "department_name", "created_by", "modified_by", "status"]

Tags: 数据pyidbymodelreturnobjectsrequestdefallrenderclassdepartmentemployees