5

We use Django for writing our new project. Our designer wants to write templates with XSLT. As you know Django has its own template system, and I don't have any arguments why the designer can't use XSLT. 2 questions:

  1. Can I return XML in my views?
  2. Is there a reason for doing this? What facts can help me changing his view on Django's templating system?
jro
  • 9,300
  • 2
  • 32
  • 37
SkyFox
  • 1,805
  • 4
  • 22
  • 33
  • 2
    i'd say your better off using xslt! a happy desginer is a productive desginer! – Treemonkey Oct 21 '11 at 09:51
  • Are you doing your XML transform on the server, in which case you don't need to return XML from a view, or on the client, in which case you might still want to use Django templates to generate the XML. Then its messy... – Spacedman Oct 21 '11 at 14:22
  • Did you end up further discussing this point w/your designers? What was their reasoning and how did things turn out? – Kreychek Jan 28 '13 at 13:05

1 Answers1

6

1). Yes, why not.

import libxslt
import libxml2

from django http import HttpResponse

def your_view(request):
    xsl = libxslt.parseStyleSheetDoc(libxml2.parseFile('stylesheet.xml'))
    data = # your xml here
    result = xsl.applyStylesheet(data)
    response = HttpResponse()
    xsl.saveResultToFile(response, result)
    return response

2). This will likely solicit opinion, debate, arguments, polling, or extended discussion. :-)

DrTyrsa
  • 31,014
  • 7
  • 86
  • 86
  • The problem is string "data = # your xml here". I think here I need to generate xml formatted data befer return. :( So many excess string in my source code :( – SkyFox Oct 21 '11 at 08:45
  • 2
    @SkyFox Yes, you need to do that extra work, so you need a reason to use XSLT instead of Django templates. "Our designer wants" doesn't sound like a good reason. – DrTyrsa Oct 21 '11 at 08:50
  • 1
    If you really have to, you can easily render xml with django's template system using render() and then pass that to the xslt. However, I strongly agree with @DrTyrsa that you need a better reason for doing it. – Tetaxa Oct 21 '11 at 09:01
  • 1
    "Our designer wants" is the best reason there is. Unless you want to do the design... – Spacedman Oct 21 '11 at 14:23
  • 1
    @Spacedman Interoperability with existing systems that use XML is the best reason. "Our designer wants" is somewhere in the middle, and can be the worst reason if it goes against how the API, framework, and rest of the design work... – Izkata Jan 08 '13 at 19:14