6

I maintain a Django webapp for a client of mine. We built it out in Django and for computer users, it's great. We now want to cater to mobile device users.

On top of a template switch, we also need things to work differently. The application will have views that work in a subtly different way but also the URL structure needs to be simplified.

I realise what I'm about to ask for violates the DRY ethos but is there a good way to split the urls.py so that half of it is for ourdomain.com and the other half is for m.ourdomain.com? If I can do that, I can add a mobile_views.py and write the new views.

Django's Sites is enabled in the project but I'm happy to use a hard-coded request.domain.startswith('m.')-style hack. Seems like that might perform better - but I've no idea how one gets the request from the URLs file.

Oli
  • 235,628
  • 64
  • 220
  • 299

1 Answers1

7

Use middleware to detect the access to the other site and set request.urlconf to the other urlconf that you want to use.

bfox
  • 274
  • 2
  • 13
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Yeah that'll work for me. Thanks. Edit: I assume this has to be one of the first middleware rules to trigger. And do you know if Django's `reverse` will still work (assuming the new URLs have the same names)? – Oli Jun 16 '11 at 10:54
  • You may have to pass `request.urlconf` to [`reverse()`](https://docs.djangoproject.com/en/dev/topics/http/urls/#django.core.urlresolvers.reverse), but otherwise it should work. – Ignacio Vazquez-Abrams Jun 16 '11 at 10:57