Django XML on Request



我有问题

在我的 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)

我宁愿不必调用 url,因为我在我的视图中调用了另一种方法。

提取check_xml()中独立于请求对象以自己的方法操作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)
    ...

最新更新