在法国的Windows Server上使用django换行的问题 [英] Problems with Newlines using django on a Windows Server in France

查看:103
本文介绍了在法国的Windows Server上使用django换行的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我的Django应用在将文件通过render_to_string读取到的字符串写入文件时,在每个CRLF之前都引入了CR.在模板文件中,我使用了CRLF,而Django处理应用程序编写了一个文件,该文件在每个CRLF之前添加了一个CR.有关代码的详细信息,请参见下文.

但是:仅当我的应用程序位于客户位于法国的Windows Server 12R2 VM上时,才会发生这种情况.我的物理Windows 10便携式计算机,我的ubuntu实例和我的AWS Windows Server 12实例(在安装在客户端计算机上之前用于测试)没有添加CR.

注意事项:出于安全原因,我只能通过远程桌面访问客户端的VM,因此,我需要与IT组进行约会,以探讨和调试问题.而且,这是9个小时的时差,并花费了他宝贵的时间.因此,我需要在我的AWS实例上重新创建此问题,以便尝试调试代码.而且,当我说它引入了CR而不是CRLF时,我可能会记错了-可能是我的代码引入了LF而不是CRLF.我一直在使用Notepad ++并显示所有字符",以便在生成的文件中查看CR(或LF)和CRLF.我认为Notepad ++表示我的错误文件是"Macintosh",而我的起始模板文件是"Dos \ Windows".

:那么,生成的文件是一个R脚本.并且,Windows R在读取第一行后给出错误,因此不喜欢CR作为换行符.如果我使用生成的R脚本并在Notepad ++中将其打开,然后将换行符(通过状态栏的右下角)更改为Dos \ Windows,则我的Rscript将可以正常工作.

更多详细信息:以下是一些代码,因此您可以准确地看到我如何读取/渲染模板以及如何将其保存到文件中.我的模板和模板dict中都没有非ASCII字符,因此我不怀疑这是unicode问题(我使用的是python 2):

rstring = render_to_string('helpers.R') + "\n" + render_to_string('Rcalc-template.R', {'debug':1})
rscript_file = os.path.join(tmpdir, 'Rcalc-rendered.R')
fh = open(rscript_file, mode="w")
with fh:
    fh.write(rstring)
subprocess.call(['Rscript', rscript_file], 
                universal_newlines=True, 
                stdout=log_filename, 
                stderr=subprocess.STDOUT, 
                shell=False)

我需要您提供的信息:如果有人对如何使我的AWS Windows 2012R2实例表现得像法国的Windows 2012R2 VM一样有想法,那将非常有帮助.我尝试将AWS实例的语言更改为法语,但这没有做到,即我的应用仅生成了带有CRLF的文件.我不太确定该从哪里去.预先感谢!

解决方案

Django版本,我有幸进入了部署了我的代码的服务器几分钟,其中有一个Django.版本差异!我部署的服务器具有Django 1.7.1,而我的测试版本具有1.8.17.因此,我去了我的测试AWS服务器,并将Django版本移回1.7.1,并且可以肯定的是,该错误是可重现的!因此,这不是Windows,本地化或语言问题...

在发行说明中,它在 File 中提到了对通用换行符的支持: https://docs.djangoproject.com/en/1.10/releases/1.8

我认为这是票证,并且已提交并修复了该票证?

https://code.djangoproject.com/ticket/8149 https://github.com/django/django/commit/eb4f6de980c5148ba48d4ed67f31cca27dd> >

我也可以通过将所有'\ r'替换为'\ n'来解决此问题:

rstring = rstring.replace("\r", "\n")

Problem: My Django app is introducing a CR before each CRLF when writing to a file a string which has been read in via render_to_string. In my template files, I used CRLFs, and the Django processing app writes a file which adds a CR before each CRLF. See below for code details.

However: this only happens with my app which is on my clients' Windows Server 12R2 VM which is located in France. My physical Windows 10 laptop, my ubuntu instances, and my AWS Windows Server 12 instance (which I use for testing before installing on my client's machine) do not add the CRs.

Caveat: For security reasons, I can only access my client's VM via remote desktop, so I need to set an appointment with the IT group in order to explore and debug the problem. And, that is a 9 hour time difference and takes his valuable time. Thus, I need to re-create this problem on my AWS instance so that I can try to debug my code. And, when I say it introduces a CR instead of CRLF, then I might be remembering it wrong -- it could be that my code introduces a LF instead of a CRLF. I have been using Notepad++ and "showing all characters" in order to see the CR (or LF) and CRLF in the generated files. I think that Notepad++ said my faulty file is "Macintosh" while my starting template file is "Dos\Windows."

So what: Well, the generated file is an R script. And, windows R dislikes CR as newlines by giving an error after reading the first line. If I take the generated R script and open it in Notepad++ and then change the newlines (via the lower right hand of the status bar) to Dos\Windows, then my Rscript will work fine.

A few more details: Here is some of the code so you can see exactly how I am reading/rendering the template and how I am saving it to a file. There are no non-ASCII chars in my template nor in the template dict, so I don't suspect it is a unicode issue (I am using python 2):

rstring = render_to_string('helpers.R') + "\n" + render_to_string('Rcalc-template.R', {'debug':1})
rscript_file = os.path.join(tmpdir, 'Rcalc-rendered.R')
fh = open(rscript_file, mode="w")
with fh:
    fh.write(rstring)
subprocess.call(['Rscript', rscript_file], 
                universal_newlines=True, 
                stdout=log_filename, 
                stderr=subprocess.STDOUT, 
                shell=False)

What I need from you: if anyone has an idea about how I can get my AWS Windows 2012R2 instance to behave like the Windows 2012R2 VM in France, then that would be super helpful. I tried changing the language of my AWS instance to French, but that didn't do it, i.e. my app generated files with CRLFs only. I'm not really sure where to go from here. Thanks in advance!

解决方案

Django versions I had a chance to remote into the server where my code was deployed for a few minutes, and there was a Django version difference! My deployed server had django 1.7.1 and my test version had 1.8.17. So, I went to my test AWS server and moved the Django version back to 1.7.1 and sure enough, the error was reproducible! So, it wasn't a Windows or localization or language issue...

In the release notes, it mentioned support in File for universal newlines: https://docs.djangoproject.com/en/1.10/releases/1.8

I think this is the ticket and commit which fixed it?

https://code.djangoproject.com/ticket/8149 https://github.com/django/django/commit/eb4f6de980c5148ba48d4ed67f31cca27dd132a8

I was also able to fix the problem by replacing all '\r' with '\n':

rstring = rstring.replace("\r", "\n")

这篇关于在法国的Windows Server上使用django换行的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆