如何将模板脚本中变量的值发送到Django中的url?

2024-04-19 22:21:02 发布

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

在django中,我想通过将任何变量的值放入模板的标记值来处理{%url%}。你知道吗

pp_001.html文件

<script>
$('#dataTables-wkgrid tbody').on( 'click', 'tr', function () {
    var data = table.row( this ).data();
    if ( $(this).hasClass('selected') ) {
       $(this).removeClass('selected');
    }
    else {
       table.$('tr.selected').removeClass('selected');
       $(this).addClass('selected');
       $.ajax({
       type: "GET",
       url: "{% url 'pp_02_open' %}",
       data : {
           "cPp_no" : data.pp_no
       },
       dataType: "json",
       cache: false,
       success: function(result){
       console.log(result) ;
       var aTag = "" ;
       $.each(result.filelist,function(index){
       cDocName  = result.filelist[index].doc_name ;                                 
       cFilename = result.filelist[index].file_name ;
       cid  = result.filelist[index].id  ;
       cpp_no = result.filelist[index].pp_no ;
       aTag += '<a href="{% url "pp_02_download" id=cid %}" class="list-group-item">'
            +'<i name="iText" class="fa fa-file-powerpoint-o fa-fw"></i>'+result.filelist[index].doc_name
            +'<i name="file_downloads" class="pull-right glyphicon glyphicon-cloud-download style="padding-left:10px">다운로드</i>'                      
            +'</span>'
            +'</a>'

</script>

你知道吗网址.py你知道吗

    path(r'^PP/download/<int:id>/$' , views_pp.download , name="pp_02_download"),

错误。。。。你知道吗

找不到关键字参数为“{id”:“'}”的“pp\u 02\u download”的反转。尝试了1个模式:['crms/^PP\/下载\/(?P[0-9]+)\/\$']

换句话说,当jsp脚本的变量值为 {%url“pp\u 02\u download id=cid%},我想知道怎么可能传递cid的值。你知道吗

如果用这种方式随机传递“10”而不是cid,它将正常执行。你知道吗


Tags: nonameidurldataindexdownloadfunction
2条回答

开发尚未完成。首先,它在参数中获取一个id值,检索数据,然后尝试下载文件。你知道吗

当前源配置为可以通过随意指定特定文件来检查下载是否正常。你知道吗

你知道吗视图.py你知道吗

def download(request,id):
    # filename = 'files/test.py'
    filename = 'files/테스트.pptx'
    file_path = os.path.join(settings.MEDIA_ROOT, filename)
    # file_path = reduce(os.path.join, (settings.MEDIA_ROOT, filename))
    logger.info('file_path : %s' , file_path )
    if os.path.exists(file_path) and os.path.isfile(file_path):
        with open(file_path, 'rb') as fp:
            response = HttpResponse(fp.read())
        content_type, encoding = mimetypes.guess_type(filename)
        if content_type is None:
            content_type = 'application/octet-stream'
        # content_type = ""
        response['Content-Type'] = content_type
        response['Content-Length'] = str(os.stat(file_path).st_size)
        logger.info('content_type : %s' , content_type )

    # encoding = 'euc-kr'
    logger.info("encoding : %s" , encoding )
    if encoding is not None:
        response['Content-Encoding'] = encoding
    else:
        response['Content-Encoding'] = 'euc-kr'

    if u'WebKit'in request.META.get('HTTP_USER_AGENT', u'Webkit'):
        filename = os.path.basename(filename)
        filename_header = 'filename=%s' % os.path.basename(filename)
    elif u'MSIE' in request.META.get('HTTP_USER_AGENT', u'MSIE'):
        filename_header = ''
        filename_header = 'filename=%s' % os.path.basename(filename)
    else:
        filename_header = 'filename=%s' % os.path.basename(filename)

    filename_header = filename_header.encode('cp949')   # utf -> euc 로 변환

    response['Content-Disposition'] = filename_header
    logger.info('filename-header : %s ' , filename_header )
    logger.info('[Content-Disposition] : %s' , response['Content-Disposition'])

    return response
path(r'PP/download/<int:id>/' , views_pp.download , name="pp_02_download"),

更改为网址.py不起作用。你知道吗

NoReverseMatch at/crms/PP编号/ 找不到关键字参数为“{id”:“'}”的“pp\u 02\u download”的反转。已尝试1个模式:['crms/PP\/download\/(?P[0-9]+)\/$']

相关问题 更多 >