I've been looking to see how to create a mobile version of a project on Webfaction.

Some prerequisites: 1. Using virtualenv 2. mod_wsgi 3. Want to use the same views and models, but different templates 4. Templates will be grabbed by changing the template_dirs setting in different settings files. For example: settings and settings_mobile

So I thought I could do all this with VirtualHosts, but I keep getting stuck trying to get the wsgi files to see settings. It just keeps displaying the wsgi hello world message.

Anyone see something I'm doing wrong in my .conf file? Please let me know what you think.

ServerRoot "/home/my_account/webapps/my_project/apache2"

LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/my_account/logs/user/access_my_project.log combined
DirectoryIndex index.py
DocumentRoot /home/my_account/webapps/my_project/htdocs
ErrorLog /home/my_account/logs/user/error_my_project.log
KeepAlive Off
Listen 123456
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 2
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5

<Directory /home/my_account/webapps/my_project/htdocs>
    AddHandler wsgi-script .py

    ServerName mydomain.com
    ServerAlias www.mydomain.com
    WSGIDaemonProcess my_project processes=5 python-path=/home/my_account/.virtualenvs/my_project/lib/python2.6/site-packages/ threads=1
    WSGIScriptAlias / /home/my_account/webapps/my_project/my_project.wsgi

    ServerAlias m.mydomain.com
    WSGIDaemonProcess my_project processes=5 python-path=/home/my_account/.virtualenvs/my_project/lib/python2.6/site-packages/ threads=1
    WSGIScriptAlias / /home/my_account/webapps/my_project/my_project_mobile.wsgi

asked 05 Nov '10, 00:59

davemerwin's gravatar image

accept rate: 0%

I think klynton means something like this as a middleware:

from django.conf import settings
import re

agents_list = [
    'Windows CE','NetFront','Klondike','PalmOS',
    '^KWC-','WAP','^KGT [NC]','iPhone',

def is_mobile(user_agent):
    for agent in agents_list:
        if re.search(agent, user_agent):
            return True
    return False

class MobileMiddleWare(object):
    def process_request(self, request):
        domain = request.META.get('HTTP_HOST', '').split('.')
        if 'm' in domain or 'mobile' in domain or is_mobile(request.META.get('HTTP_USER_AGENT','')):
            settings.TEMPLATE_DIRS = settings.MOBILE_TEMPLATE_DIR
            settings.TEMPLATE_DIRS = settings.DESKTOP_TEMPLATE_DIR

This way you can run one django app but still use two sets of templates.

permanent link

answered 05 Nov '10, 02:16

lamusoftware's gravatar image

accept rate: 16%

That is exactly what I meant! Thank you!

(05 Nov '10, 02:30) klynton klynton's gravatar image

Oh, that is RAD. Then I don't have to futz with apache. I'm going to play with that. Thanks for the idea.

(05 Nov '10, 17:23) davemerwin davemerwin's gravatar image

I suppose my initial concern with this is, if the site is under heavy traffic, is there a possibility that the templates dir gets out of whack and mobile users are being pointed to the desktop version.

And worse Django is looking for templates in the mobile_dir for desktop users / vice versa and certain templates may not exist in either directory and throws 500 errors.

Is that even possible?

(03 Jun '11, 13:41) esatterwhite esatterwhite's gravatar image

That seems very unlikely, even impossible, to me.

(03 Jun '11, 14:54) seanf ♦♦ seanf's gravatar image

Hi Dave,

There is nothing in your configuration that looks glaringly wrong, however, most of the Django mobile setups I have seen use a middleware that looks at the browser agent and grabs the templates or directs the browser to the right templates.

permanent link

answered 05 Nov '10, 01:16

klynton's gravatar image

accept rate: 42%

but doesn't that mess up template inheritance? Meaning, you'd have to have a ton of complicated if checks to display template blocks based on the browser agent? So then everything would have to be in templates and you couldn't have a separate mobile directory? Or am I being over simplified?

With this method I can have a mobile dir be the "templates" dir and then all templates could inherit from that dir, creating a completely isolated template branch.

Yes, I do have to do some copy and pasting, but it's not too painful and helps keep things clean.


(05 Nov '10, 01:53) davemerwin davemerwin's gravatar image


I'm not quite sure how the middleware handles inheritance. From what I've seen of middlewares that do this is that they have an entirely different set of templates, that are simpler and don't have as much information in them.

It looks like you've got a good solution, as well. For the solution to your settings problem, there is some good information here:


On running multiple django apps with different WSGI files from the same apache.

(05 Nov '10, 02:14) klynton klynton's gravatar image

Just for the record. I have abandoned this method in favor of http://www.stacklayout.com/

permanent link

answered 03 Jun '11, 15:10

lamusoftware's gravatar image

accept rate: 16%

edited 03 Jun '11, 15:11

I'm going to build a mobile version of my website on webfaction, is there any Middleware help me do that except minidetector?

Take a look: http://truyenyy.com/

permanent link

answered 14 Aug '13, 23:49

toibadao's gravatar image

accept rate: 0%

edited 14 Aug '13, 23:49

The answer above my lamusoftware might be just what you need, you should try that, or stacklayout which they have recommended since.

(15 Aug '13, 01:30) waynek waynek's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 05 Nov '10, 00:59

question was seen: 12,126 times

last updated: 15 Aug '13, 01:30

Features & prices    Sign up    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
© Copyright 2003-2015 Swarma Limited - WebFaction is a service of Swarma Limited