login community faq
2
1

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
NameVirtualHost 123.123.123.123:123456
ServerLimit 2
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5

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

<VirtualHost 123.123.123.123:123456>
    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
</VirtualHost>

<VirtualHost 123.123.123.123:123456>
    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
</VirtualHost>

asked Nov 05 '10 at 00:59

davemerwin's gravatar image

davemerwin
2613


I think klynton means something like this as a middleware:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from django.conf import settings
import re

agents_list = [
    'Nokia','bMOT','^LGE?b','SonyEricsson',
    'Ericsson','BlackBerry','DoCoMo','Symbian',
    'Windows CE','NetFront','Klondike','PalmOS',
    'PalmSource','portalmm','S[CG]H-','bSAGEM',
    'SEC-','jBrowser-WAP','Mitsu','Panasonic-',
    'SAMSUNG-','Samsung-','Sendo','SHARP-',
    'Vodaphone','BenQ','iPAQ','AvantGo',
    'Go.Web','Sanyo-','AUDIOVOX','PG-',
    'CDM[-d]','^KDDI-','^SIE-','TSM[-d]',
    '^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
        else:
            settings.TEMPLATE_DIRS = settings.DESKTOP_TEMPLATE_DIR

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

answered Nov 05 '10 at 02:16

lamusoftware's gravatar image

lamusoftware
1679

That is exactly what I meant! Thank you!

(Nov 05 '10 at 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.

(Nov 05 '10 at 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?

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

That seems very unlikely, even impossible, to me.

(Jun 03 '11 at 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.

answered Nov 05 '10 at 01:16

klynton's gravatar image

klynton ♦♦
148128

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.

Thoughts?

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

Hi,

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:

http://forum.webfaction.com/viewtopic.php?id=3646

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

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

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

answered Jun 03 '11 at 15:10

lamusoftware's gravatar image

lamusoftware
1679

edited Jun 03 '11 at 15:11

Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Tags:

×641
×44
×26
×2

Asked: Nov 05 '10 at 00:59

Seen: 2,151 times

Last updated: Jun 03 '11 at 15:11

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited