-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?

0

3 Answers 3

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.

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

1 Comment

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')
1

In views.py:

import xmlparser

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

xmlparser.your_method

4 Comments

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
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
Django responses me this exception:
Django answered me this: Exception value: 'module' object has no attribute 'process'
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

1 Comment

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

Your Answer

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