将变量从模板传递到django中的视图

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

在点击每个位置时,我必须从那里获取文本文件。我需要将这些位置传递给views.py来呈现文件。

模板:

<script>
if(data.columns[j] == "outputFilePath"){
    var op = JSON.stringify(data.tableData[i][j]);
    op = op.substring(1, op.length-1)
    row.append("<td><a href='/dhl/outputDir'>" + op + "</a></td>")
}
</script>

观点。潘岳:

def outputDir(request,location):
    text_data = open("location/stdout", "rb").read()
    return HttpResponse(text_data, content_type="text/plain")

URLs.朋友

url(r'^dhl/outputDir',views.outputDir),
django django-views django-urls
1个回答
0
投票

您可以通过传递参数来使用基本模板标记和默认视图功能。

但是,您要实现的目标是,通过输入他们想要的任何文件夹,任何人都可以通过访问应用程序中的文件夹来打开视图。您可以添加允许的位置列表,我在下面的解决方案中已经完成了。

模板

if(data.columns[j] == "outputFilePath"){
    var op = JSON.stringify(data.tableData[i][j]);
    op = op.substring(1, op.length-1)
    row.append("<td><a href='/dhl/outputDir/" + op + "'>" + op + "</a></td>")
}

views.朋友

def outputDir(request, location):
    # Make sure to check if the location is in the list of allowed locations
    if location in allowed_locations:
        text_data = open(location + "/stdout", "rb").read()
        return HttpResponse(text_data, content_type="text/plain")
    else:
        return PermissionDenied

您还需要在url中添加一个参数:

URLs.朋友

url(r'^dhl/outputDir/(?P<location>\w+)', views.outputDir),
© www.soinside.com 2019 - 2024. All rights reserved.