试图读取django中的文本文件

问题描述 投票:0回答:1

我无法从view.py中读取文件的内容

下面是我的代码:

def home_view (request, *args, **kwargs):
    ksh_test_result=AutomationTestResult.objects.values('tatr2tafmc__jobcommand', 'ksh_completion','ftp_log_abs_path','aftp_log_abs_path').distinct()
    ksh_drilldown_data=AutomationTestResult.objects.all()
    for ksh in ksh_test_result:
        ftp_log_file[ksh.tatr2tafmc__jobcommand]=open(ksh.ftp_log_abs_path, 'r').read()
        aftp_log_file[ksh.tatr2tafmc__jobcommand]=open(ksh.aftp_log_abs_path, 'r').read()
    print(ftp_log_file)
    print(aftp_log_file)
    context={
        "ksh_list" : ksh_test_result,
        "ksh_drilldown" : ksh_drilldown_data,
        "ftp_log" : ftp_log_file,
        "aftp_log" : aftp_log_file
    }
    return render(request, "home.html", context)

我正在从数据库中读取文件的路径。当我运行此代码时,出现以下错误代码

Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Internal Server Error: /
Traceback (most recent call last):
  File "/home/nmehta/Projects/GATI/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/nmehta/Projects/GATI/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/nmehta/Projects/GATI/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/nmehta/Projects/GATI/src/KTA/dashboard/views.py", line 11, in home_view
    ftp_log_file[ksh.tatr2tafmc__jobcommand]=open(ksh.ftp_log_abs_path, 'r').read()
AttributeError: 'dict' object has no attribute 'ftp_log_abs_path'
[20/Nov/2019 14:31:27] "GET / HTTP/1.1" 500 65923
python django django-queryset readfile
1个回答
0
投票

values返回dict的查询集,而不是模型实例;在open(ksh.ftp_log_abs_path, 'r')中,您尝试访问ftp_log_abs_path作为dict ksh的属性。

© www.soinside.com 2019 - 2024. All rights reserved.