2011-05-20 41 views
0

我有一個問題Django XML的要求

在我的views.py我有一個方法,從POST採取XML並做一些東西。

def check_xml(request): 
    try: 
     # get the XML records from the POST data 
     xml = request.raw_post_data 

這個偉大的工程,我可以使用測試:

xml_data = """<root><a><b>Hello</b><\a></root>""" 
h = Http() 
resp, content = h.request("http://myurl/check_xml", "POST", xml_data) 

然而,在我看來,我有我想打電話給check_xml()

# i construct some xml using lxml.etree 
myrequest.raw_post_data = new_xml 
check_xml(myrequest) 

其他功能我寧願不必調用網址,因爲我在我的視圖中調用另一種方法。

+0

如果我理解正確的,你想在調用'check_xml()'從另一種方法你'views.py'而無需發佈到'HTTP請求:// myurl/check_xml'? – manji 2011-05-20 13:12:29

+0

是的。也許我應該有第二個可選的參數def check_xml(request,xml = None) – Mark 2011-05-20 13:13:32

回答

2

提取操縱XML對象在其自己的方法獨立於請求對象的check_xml()部分:

def xml_function(xml): 
    #do what you have to do with the `xml` arg 
    ... 

的調用它在check_xml()和任何其它方法(直接調用(無請求))。

def check_xml(request): 
    try: 
     # get the XML records from the POST data 
     xml = request.raw_post_data 
     ... 
     xml_function(xml) 
     ... 

def other_function(): 
    ... 
    xml_function(new_xml) 
    ... 
+0

謝謝。我是個白癡! – Mark 2011-05-20 14:04:22

+0

@Mark,將manji的回答作爲答案。這可以讓其他位於同一條船上的StackOverflow用戶知道這裏有可用的驗證答案。 – 2011-05-20 14:48:10