[英]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.