-1

I've xmlparser.py. This .py parses a local XML file and then create/update objects from models SQLITE3 database.

This xmlparser.py file is in the same folder as views.py. Well, I wanna "execute" xmlparser.py (which update my db) and then get in views.py database objects updated for HttpResponse.

How could I do that?

3 Answers3

3

I guess whatever you do in xmlparser.py is done with classes and functions. you should be able to import it and use those methods in the current python file.

Gocht
  • 9,924
  • 3
  • 42
  • 81
  • There's no error... the code.. A is just an example. import xml.sax from models import Actividad ... def characters (self, content): ... A = Actividad.objects.create(titulo = "hola2", tipo = "tipo", precio=2.3, fecha="2015-03-04", hora="19:23") if __name__ == '__main__': parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) Handler = MyHandler() parser.setContentHandler(Handler) parser.parse('your.xml') –  May 20 '15 at 21:28
1

In views.py:

import xmlparser

Then just use it as other python modules. E.g.

xmlparser.your_method
moonstruck
  • 2,729
  • 2
  • 26
  • 29
  • I've no method, I just wrote xmlparser Inside xmlparser I have something like this A = Actividad.objects.create(titulo = "hola2", tipo = "tipo", precio=2.3, fecha="2015-03-04") and makes no sense. If I write it in views.py directly it works, but it doesn't inside xmlparser. In views.py I have import xmlparser In xmlparser I have from models import Actividad –  May 20 '15 at 17:39
  • To execute xmlparser in views function you may move xmlparser.py statements in some function and later call it. Using subprocess is another option. Check [@Josep Valis's answer](http://stackoverflow.com/a/30356645/303182) – moonstruck May 20 '15 at 17:49
  • Django responses me this exception: –  May 21 '15 at 14:45
  • Django answered me this: Exception value: 'module' object has no attribute 'process' –  May 21 '15 at 14:45
1

You can execute anything on the OS with:

from subprocess import call
call(["python","xmlparser.py"])

Also look at the second answer here:

using python subprocess call to invoke python script

Community
  • 1
  • 1
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • I think that is a Django issue I've no method, I just wrote xmlparser Inside xmlparser I have something like this A = Actividad.objects.create(titulo = "hola2", tipo = "tipo", precio=2.3, fecha="2015-03-04") and makes no sense. If I write it in views.py directly it works, but it doesn't inside xmlparser. In views.py I have import xmlparser In xmlparser I have from models import Actividad –  May 20 '15 at 17:41