How do I create a standalone python application that uses the django environment (e.g. the ORM etc...)?

1,305 views
Skip to first unread message

DJ-Tom

unread,
Sep 16, 2013, 10:25:45 AM9/16/13
to django...@googlegroups.com
For reporting purposes I want to use List & Label from Combit. The web/online part is no big deal since the reporting module can be run without user interface (creating Excel or PDF files)

But the actual form designer part that is used to define the report layout is a windows GUI application, so I would like to have a python file inside my Django app that I can call to start the designer part on my local machine.

I have now read a bunch of descriptions on how to use Django in a standalone application but did not succeed so far in actually running my form designer.

This is what I currently have, i tried to start this via "python formdesigner.py", after activating the correct virtualenv:

from Tkinter import *
from ctypes import *
import django
from models import *

LL = windll.cmll18

### Declaration of application class omitted, this is plain python  ####

app = Application()
app.master.title("List & Label sample application")
app.mainloop()


Regardless what I try, I can't get the model class import to work.

I also tried SET DJANGO_SETTINGS_MODULE=settings and project.settings and project.app.settings... nothing worked.

thomas

Nigel Legg

unread,
Sep 16, 2013, 11:19:09 AM9/16/13
to django...@googlegroups.com
Shouldn't it be

       from django.db import models

??

Cheers, Nigel
07914 740972



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Brad Pitcher

unread,
Sep 16, 2013, 12:17:21 PM9/16/13
to django-users
You need to do something like this before import django stuff:

import os import sys sys.path.append(os.path.abspath(os.path.dirname(__file__))) os.environ['DJANGO_SETTINGS_MODULE'] = 'web.settings'

---------
Brad Pitcher

Avraham Serour

unread,
Sep 16, 2013, 12:38:35 PM9/16/13
to django...@googlegroups.com
the question is what for? other than the ORM what else is useful for a desktop app?
if it is only the ORM you want take a look at http://www.sqlalchemy.org/

DJ-Tom

unread,
Sep 17, 2013, 3:25:41 AM9/17/13
to django...@googlegroups.com

Hi,

I'm also not sure if the python file is at the correct location.

This is how the directory structure looks:

my_project
    \my_project\
         settings.py
    \my_app\ # this is where my models are defined
         models.py
         formdesigner.py


Where should my formdesigner.py be located?

my_project ?
my_project\my_project ?
my_project\my_app ?

What do I put into DJANGO_SETTINGS_MODULE?

'my_project.settings'?

DJ-Tom

unread,
Sep 17, 2013, 9:18:21 AM9/17/13
to django...@googlegroups.com
I need to use the ORM and more specifcally I want to use the models I defined inside my Django application.

I want to avoid using a different ORM like Alchemy because then I would have to duplicate the work I already put into my Django models - i hate duplicating work...

Apart from that, I don't need much else (maybe the template engine for sending emails...)

The formdesigner application should live inside the web application structure so I can manage it the same way as I'm doing it with the web application and have it included in the same Subversion project.

Thomas

Brad Pitcher

unread,
Sep 17, 2013, 11:58:42 AM9/17/13
to django-users
To make my code work, you could set DJANGO_SETTINGS_MODULE to 'my_project.settings', and just put formdesigner.py directly under my_project.

If you wanted to keep formdesigner.py where it is, you would want to change the sys.path.append line to this:

sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))


---------
Brad Pitcher


Russell Keith-Magee

unread,
Sep 17, 2013, 8:05:41 PM9/17/13
to Django Users
Hi,

Django's own documentation contains a description of how to do this:

https://docs.djangoproject.com/en/1.5/topics/settings/#using-settings-without-setting-django-settings-module

James Bennett has also written a blog post that gives a good rundown on approaches you can take:


Unfortunately, the links in that blog post are a little stale; however, he provides code samples, which should be enough to get you going.

Yours,
Russ Magee %-)

DJ-Tom

unread,
Sep 18, 2013, 3:13:09 AM9/18/13
to django...@googlegroups.com
Hi Brad,

that fixed it - plus I now have a better understanding of how python works.

I'm beginning to understand what Jeff Knupp writes here: http://www.jeffknupp.com/blog/2012/12/11/learning-python-via-django-considered-harmful/

:-)

Sells, Fred

unread,
Sep 23, 2013, 2:26:26 PM9/23/13
to django...@googlegroups.com

Creating django management commands is easy and then all works nicely.  Assuming you’re at 1.5, not sure about older ones.  Just google it.

--

Reply all
Reply to author
Forward
0 new messages