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

时间:2012-08-23 13:38:54

标签: javascript python html django

用户在textarea(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>

似乎用"application/x-www-form-urlencoded"对数据进行编码不足以对换行符进行编码,

这是我将数据发送到其他应用程序的方法:

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) 

因此,当用户输入python代码时,例如;

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

这是收到的:

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

没有换行符编码。 我尝试从键盘捕获<enter>事件,并将\n添加到文本区域的值,但它没有用。

感谢。

1 个答案:

答案 0 :(得分:0)

在将数据附加到网址之前,您可能需要urllib.urlquote()数据:

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