[Django]-Avoiding redundant code in mobile/desktop with Django sites framework

2👍

Have you looked at this app: http://pypi.python.org/pypi/django-mobility?

Using a middleware for detecting the device and decorators to switch templates depending on the incoming device are a good approach to avoid redundant if/else constructs.

And if you look at the examples given for django-mobility they look pretty similiar to your desired construct:

def view(request):
    ...

@mobilized(view)
def view(request):
   ...
👤arie

1👍

You can use middleware to detect whether or not the request is to the ‘m’ subdomain or not, and then specify the correct URL conf to direct you to the views you want. I’ve been using a modified version of the django-subdomains app for this and it’s been working nicely. This is an effective and simple solution if your view logic for your mobile site is quite different from the view logic of your regular site. Here’s the link:

https://github.com/tkaemming/django-subdomains

Then all you have to do is write a new URL conf for your mobile site, specify this in your settings, and then write your views/templates for your mobile site just like you would for your regular app.

👤Spike

Leave a comment