Package Django applications as desktop programs that can be run independently

Source: Internet
Author: User
Tags auth i18n sessions
Package Django applications as desktop programs that can be run independently

Requirements: Run the Django+sqlite application on the PC without having to install the Django Python database Web server ...

Traditional Django applications run in Apache or Nginx fastcgi, and hypocrisy makes it easy for us to use CherryPy as a Web server.

Cx_freeze python packaging tools across the platform

Steps:

1: Download and install Cx_freeze CherryPy

2 Dependencies advance import of project-dependent packages

Import Django.contrib.auth
Import Django.contrib.contenttypes
Import Django.contrib.sessions
Import Django.contrib.sites
Import Django.contrib.admin



#these pertain to your application
Import Subway.models
Import Subway.views
Import URLs
Import Manage
Import settings

Import Django.db.models.sql.compiler
From django.contrib.auth.backends Import *
From django.conf.urls.defaults Import *
#these are Django imports
Import Django.template.loaders.filesystem
Import Django.template.loaders.app_directories
Import Django.middleware.common
Import Django.contrib.sessions.middleware
Import Django.contrib.auth.middleware
Import Django.middleware.doc

Import Django.contrib.messages
Import Django.contrib.staticfiles



Import Django.contrib.messages.middleware
Import django.contrib.sessions.backends.db
Import Django.contrib.messages.storage.user_messages

Import Django.db.backends.sqlite3.base
Import Django.db.backends.sqlite3.introspection
Import Django.db.backends.sqlite3.creation
Import Django.db.backends.sqlite3.client


Import Django.contrib.auth.context_processors
From django.core.context_processors Import *

Import Django.contrib.messages.context_processors

Import Django.contrib.auth.models
Import Django.contrib.contenttypes.models
Import Django.contrib.sessions.models
Import Django.contrib.sites.models
Import Django.contrib.messages.models
Import Django.contrib.staticfiles.models
Import Django.contrib.admin.models




Import Django.template.defaulttags
Import Django.template.defaultfilters
Import Django.template.loader_tags

#dont need to import pkgs
#need to know how to exclude them
Import Email.mime.audio
Import Email.mime.base
Import Email.mime.image
Import Email.mime.message
Import Email.mime.multipart
Import Email.mime.nonmultipart
Import Email.mime.text
Import Email.charset
Import Email.encoders
Import Email.errors
Import Email.feedparser
Import Email.generator
Import Email.header
Import Email.iterators
Import Email.message
Import Email.parser
Import Email.utils
Import Email.base64mime
Import Email.quoprimime
Import Django.core.cache.backends.locmem
Import django.templatetags.i18n
Import Django.templatetags.future
Import django.views.i18n

Import Django.core.context_processors


Import Django.template.defaulttags
Import Django.template.defaultfilters
Import Django.template.loader_tags


From django.conf.urls.defaults Import *
Import Django.contrib.admin.views.main
Import Django.core.context_processors
Import Django.contrib.auth.views
Import Django.contrib.auth.backends
Import django.views.static


Import Django.contrib.admin.templatetags.log

Import Django.contrib.admin.templatetags.adminmedia




Import Django.conf.urls.shortcut
Import Django.views.defaults

#let US hook up cherrypy
#is it possible to hook up the dev server itself?

Import CherryPy
From CherryPy import Wsgiserver
From Django.core.handlers.wsgi import Wsgihandler
From django.core.servers.basehttp import Adminmediahandler
From django.conf Import settings
From django.utils import translation

3: Write the setup file

From Cx_freeze import Setup, executable
Import Cx_imports
Setup
Name = "Ehome",
Version = "1.0",
Description = "The typical ' Hello, world! ' script",
Options = Dict (Build_exe = {' Include_files ': [' templates ', ' Media ']}), #拷贝 Admin template resource file
executables = [Executable ("ehome.py")])

4: Replace the urls.py admin.autodiscover () into

Import Django.contrib.auth.admin
#import django.contrib.contenttypes.admin

Import Django.contrib.sites.admin

Otherwise there is no permission to access the background prompts

5: Start the service

Import CherryPy
From CherryPy import Wsgiserver
From Django.core.handlers.wsgi import Wsgihandler
From django.core.servers.basehttp import Adminmediahandler
From django.conf Import settings
From django.utils import translation
if __name__ = = "__main__":



# Django.core.management.base forces the locale to en-us. We should
# set it up correctly for the ' the ' particularly important
# in the '--noreload ' case).
Translation.activate (settings. Language_code)

Try
Path = './media '
Handler = Adminmediahandler (Wsgihandler (), Path)
Cherrypy.config.update ({
' Server.socket_host ': ' localhost ',
' Server.socket_port ': 8000,
' Log.error_file ': ' Site.log ',
' engine.autoreload_on ': True
})

Cherrypy.config.update ({' Environment ': ' Production ',
' Log.error_file ': ' Site.log ',
' Log.screen ': False})

Cherrypy.tree.graft (Handler, '/')

Cherrypy.engine.start ()
Cherrypy.engine.block ()
Finally

Cherrypy.engine.exit ()

Summary: There are many problems in the middle

1:cx_freeze documents are rarely

2:django used a lot of dynamic loading module to manually one by one import in the place

Reference:

http://www.jjude.com/2008/04/make-your-django-application-as-a-stand-alone-desktop-application/

Http://toolsforagile.com/blog/archives/51

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.