Last Updated: February 25, 2016
·
773
· r0sk

Go mobile on Django

Try it out in almost 5 mins, It's kinda easy:

$ pip install minidetector

Install it on your Django project, settings.py:

MIDDLEWARE_CLASSES = (
    [...]
    'minidetector.Middleware'
)
[...]
INSTALLED_APPS = (
    [...]
    'minidetector',
)

And lastly you can use it in a function view:

def index(request, template_name='index.html'):
    if request.mobile:
        template_name='mobile/index.html'

Or in a Class Based View:

class index(CreateView):
    template_name = "index.html"
    context_object_name = "whatever"

    def get_context_data(self, **kwargs):
        if self.request.mobile:
            self.template_name='mobile/index.html'

And that's all folks!