简体   繁体   English

从一个Django应用程序向另一个应用程序发送数据时,如何保持换行符

[英]How can I keep line breaks when sending data from one django application to another

A user write a code in a textarea (HTML) I post the form like this: 用户在文本区域(HTML)中编写代码,我将表单发布为:

<form id="codeid" method="post" enctype="application/x-www-form-urlencoded" name="code" action="192.168.56.2:8000/api/comp/">
    <input id="textarea_1" name="content" cols="80" rows="15" type="text" onkeypress="return runScript(event)"></input>
    <input id="thebutton" type="button" value="Submit"  onclick="document.forms.codeid.submit();" /> 
</form>

It seems that encoding the data with "application/x-www-form-urlencoded" is not enough to encode the line breaks, "application/x-www-form-urlencoded"编码数据似乎不足以编码换行符,

This is how I do to send the data to the other application: 这是我将数据发送到另一个应用程序的方式:

def comp(request):

    data = request.POST['content']
    url = urllib2.urlopen('http://192.168.56.2:8000/api/comp/?' + data)
    redirect('http://192.168.56.2:8000/api/comp/?' + data)
    tml = url.read()

    return HttpResponse(encoded_data) 

So when the user type a python code for example; 因此,例如,当用户键入python代码时;

def current_datetime(request):
    current_date=datetime.datetime.now()
    return render_to_response('index.html', locals())

This is what's received: 这是收到的:

def+current_datetime%28request%29%3A++++current_date%3Ddatetime.datetime.now%28%29++++return+render_to_response%28%27index.html%27%2C+locals%28%29%29

There is no encoding for line breaks. 没有用于换行符的编码。 I tried to capture the <enter> event from the keyboard and add \\n to the value of the text-area but it didn't worked. 我试图从键盘上捕获<enter>事件,并将\\n添加到文本区域的值,但这没有用。

Thanks. 谢谢。

You probably need to urllib.urlquote() the data before appending it to the URL: 在将数据附加到URL之前,您可能需要urllib.urlquote()

>>> import urllib
>>> urllib.quote('\n')
'%0A'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 粘贴到MS Word中后,如何在Javascript中格式化剪贴板数据以创建软换行符? - How can I format clipboard data in Javascript to create soft line breaks when pasted into MS Word? 调用insertText时如何防止插入换行符? - How can I prevent line breaks from being inserted when calling insertText? Textarea .val通过Ajax发送数据时没有换行符 - Textarea .val No Line Breaks when Sending Data via Ajax 如果附加带有发布的html的div,我如何保持换行符不变? - How do I keep the line breaks intact, when appending a div with posted html? 如何使用ajax通过URL发布消息并仍保持换行符? - How can I use ajax to post message through a url and still keep line breaks? 如何通过 contenteditable div 输入保留附加到列表的元素的换行符(格式)? - How can I keep the line breaks (formatting) of an element appended to a list via a contenteditable div input? 发送邮件时保留文本区域的换行符 - Preserving line breaks from text-area when sending mail 从一页跳转到另一页时如何使用localStorage中的数据? - How can I use data in localStorage when jumping from one page to another? 如何从一行中分离数据 - How can I separate data from one line 通过帖子发送文本时如何不丢失换行符 - How to not lose line breaks when sending a text via post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM