[Flask] Trying to pass XML through to an HTML template

David Lord davidism at gmail.com
Wed Dec 2 12:27:11 EST 2015


You passed `xmlitem` in `render_template`, so it's not clear why you're 
trying to access it from `request.args`.  Since it's an etree node, 
you'll need to call [`.toString`][1] on it to convert it first. 
Reference it as the variable you passed to the context: `{{ 
xmlitem.toString() }}`.

[1]: 
https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring

On 12/02/2015 09:07 AM, Dunlap, Zachariah wrote:
> Hi all,
>
> I’m sure I’m missing something but I haven’t had any luck figuring out
> what, perhaps someone here can point me in the right direction.
>
> I have the module below which I’m using to get and XML item from a URL,
> read it and return elements and values to an HTML template.
>
> The itemid and description variables render in the template just fine,
> the one that I’m having an issue with is the XML I need to send through
> to a textarea on the template. I’ve tried rendering it as a string,
> escaping it & encoding it but have had no luck getting it passed through.
>
> I’ve tested getting this XML in a simple module and I know it returns
> content but the module for this applicattion can’t see that value, it
> comes back as ‘None’.
>
> Any guidance on how I can pass XML through to the textarea would be
> greatly appreciated.
>
> MODULE:
>
> @app.route('/updateitem', methods=['GET'])
>
> def updateitem():
>
>      url = 'http://xmldocument.fake'
>
>      url_request = requests.get(url)
>
>      root = etree.fromstring(url_request.text)
>
>      itemid = root.xpath('//main/@id')
>
>      description = root.xpath('//main/text()')
>
>      xmlitem = root.xpath('//ns:XmlItem', namespaces={'ns':
> 'http://fakenamespace.com'})
>
>      # return render_template('create-definitions.html', itemid=itemid,
> name=name, description=description, xmlitem=xmlitem)
>
>      return render_template('create-definitions.html', **locals())
>
> TEXTAREA NEEDING THE XML:
>
> <textarea rows="20" name="xmlitem" id="xmlitem" >{{
> request.args.get('xmlitem') }}</textarea>
>
> Thanks,
>
>
> Zach
>
> The information contained in this communication is intended for the use
> of the designated recipients named above. If the reader of this
> communication is not the intended recipient, you are hereby notified
> that you have received this communication in error, and that any review,
> dissemination, distribution or copying of this communication is strictly
> prohibited. If you have received this communication in error, please
> notify The Associated Press immediately by telephone at +1-212-621-1898
> and delete this email. Thank you.
> [IP_US_DISC]
>
>
> msk dccc60c6d2c3a6438f0cf467d9a4938
>
>
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>


More information about the Flask mailing list