0

I am using Django framework and python scripts for validating XML files.

I usually parse the XML file, which is already present in the defined location, using the below code.

import xml.etree.ElementTree as ET
tree = ET.parse('config.xml')

However, now I want to add the file dynamically from the front end browse file option and place it in the above ET.parse('file.xml') location. What is the best way to achieve this?

1 Answer 1

2
<form action="/handle_xml_upload/" enctype="multipart/form-data" method="post">
    {% csrf_token %}
    <input type="file" name="xmlfile">

    <input type="submit" value="upload xml file">
</form>

and in views.py

def handle_xml_upload(request):
    xmlfile = request.FILES['xmlfile']
    tree = ET.parse(xmlfile)
    # ...

of course, you need adjust your urls.py as well ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.