简体   繁体   中英

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:

<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,

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;

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.

Thanks.

You probably need to urllib.urlquote() the data before appending it to the URL:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM