开发者

Django XML on Request

I have a problem

In my views.py I have a method that takes xml from the POST and does some stuff.

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

This works great I can test it using:

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

However, In my view I have another function which I want to call check_xml()

# i construct some xml using lxml.etree
myrequest.r开发者_开发知识库aw_post_data = new_xml
check_xml(myrequest)

I would rather not have to call the url, seeing as i am calling another method in my views.


Extract the part of check_xml() that manipulates the XML object in its own method independent from the request object:

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

the call it in check_xml() and in any other method (direct call (no requests)).

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜