如何将数据从一个Django应用程序发送到另一个?

问题描述:

这是我的问题。我需要将表单从一个Django应用程序发送到另一个(在不同的机器上)。考虑到换行符,这是我如何发布数据。

Here is my issue. I need to send a form from one Django application to another (on separate machines). Taking into consideration line breaks, this is how I post my data.

<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"></input>
    <input id="thebutton" type="button" value="Submit"  onclick="document.forms.codeid.submit();" /> 
</form>

表单动作即 action =192.168.56.2:8000/api/ comp / 由url.py处理:

The form action i.e. action="192.168.56.2:8000/api/comp/ is handled by url.py:

urlpatterns = patterns('',
    (r'^time/$', current_datetime),
    (r'^time/192.168.56.2:8000/api/comp/$', comp2),
)
urlpatterns += staticfiles_urlpatterns()

在views.py中,我不知道要发送表单的内容我尝试从表单中提取数据并发送这样的URL:

In views.py I don't know exactly what to write to send the form to the other application. I tried to extract the data from the form and send it with the URL like this:

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



但是我失去了线路断裂。

but I lose the linebreaks.

我认为你可以用更好的方式发送数据,例如使用HTML表单直接发送数据到目的地服务r等等

I think you can send data in better ways, for example by using an HTML form to directly send data to the destination server, etc.

无论如何,将数据与 urllib2.urlopen 行中的URL混合的方法非常糟糕当然,你会丢掉换行符和许多其他字符。在将数据添加到URL之前,您应该对数据进行编码,如下所示:

Anyway your approach to mix data with the URL in the urllib2.urlopen line is very bad, and of course you'll lose line breaks and many other characters. You should encode your data before adding it to the URL, like this:

encoded_data = urllib.urlencode({'content': 'Hello,\nIt is a sample content!'})
url = urllib2.urlopen('http://192.168.56.2:8000/api/comp/?' + encoded_data)