【问题标题】:How to get employee id using excel sheet如何使用excel表格获取员工ID
【发布时间】:2021-09-24 22:51:20
【问题描述】:

我创建了一个系统(在 Django 中),每当我将 excel 文件上传到网站时,它都会被加载到数据库中。

如何从数据库中检索 EmpID 并在 HTML 中显示?

这是我的观点.py

def upload_file_view(request):
    form = EmployeeForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        obj = form.save()
        form = EmployeeForm()
        with open(obj.file_name.path, 'r') as f:
            reader = csv.reader(f)
            
            next(reader)
            for row in reader:
                print(row)
                EmpID = row[1].upper()
                EmpName = row[2].upper()
                EmpEmail = row[3].upper()
                Employee.objects.create(
                    EmpID = EmpID,
                    EmpName = EmpName,
                    EmpEmail = EmpEmail,
                )

            obj.activated = True
            obj.save()
            
    return render(request, "EmpDBUpload/upload.html", {'form':form})

这是我的 Viewclaim.html

 <h1> Emp id: {{EmpID}} </h1>

这是我的 ViewClaim 的 views.py

def ViewClaim(request):
  context = initialize_context(request)
  user = context['user']
  claims = Claims.objects.all()
  
  emp_ids = Employee.objects.values('EmpID')
  context = {'emp_ids': emp_ids}

  return render(request, 'User/ViewClaim.html', {'date': x, 'user':user, 'claims':claims} ,context=context)

【问题讨论】:

  • 您想从 Excel 或 DB 中检索 EmpID 吗?
  • 无论哪个能显示
  • 从数据库中检索它,因为您已经在数据库中拥有数据。编写查询以获取视图中的 EmpID。
  • 如何编写查询以检索到 html 中?
  • 请发布视图代码(您将 EmpID 发送到 html 的位置),以便我帮助您。

标签: python django excel django-views django-templates


【解决方案1】:

在你的ViewClaim

def ViewClaim(request): 
    context = initialize_context(request) 
    user = context['user'] 
    claims = Claims.objects.all()
    # Fetches the employee ids of all Employees
    emp_ids = Employee.objects.all() 
    
    return render(request, 'User/ViewClaim.html', {'date': x, 'user':user, 'claims':claims, 'emp_ids': emp_ids})

在您的 Viewclaim.html

{% for emp in emp_ids %}
    <h1> Emp id: {{ emp.EmpID }} </h1>
{% endfor %}

【讨论】:

  • 我在 /upload 处收到 TypeError
    render() 为参数 'context' 获得了多个值
  • 您是否将此代码添加到upload_file_view 视图?
  • 是的,我在 upload_file_view 中添加了 emp_id,在 html 中添加了 for 循环
  • 我清楚地提到您必须将此代码添加到服务于 Viewclaim.html 的视图中,而不是添加到 upload_file_view。您如何将数据发送到 Viewclaim.html ?那是什么观点?
  • 我想你搞错了。 upload_file_view 链接到 upload.html 但我希望它显示在 Viewclaim.html 上,因为它是一个单独的项目
猜你喜欢
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
相关资源
最近更新 更多