簡體   English   中英

從一個Django應用程序向另一個應用程序發送數據時,如何保持換行符

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

用戶在文本區域(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添加到文本區域的值,但這沒有用。

謝謝。

在將數據附加到URL之前,您可能需要urllib.urlquote()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM