It sounds like you want to automagically do something like:

if request.is_mobile:
  return render_to_response('test_mobile.html',{})
return render_to_response('test.html',{})

I'm not sure that will be possible without modifying your views.  I did 
something similar by determining which base template all my templates 
should extend.  Here's what I did:

-Set request.is_mobile if the user is using a mobile browser.  I happened 
to use the same middleware you posted but tweaked it to allow viewing the 
desktop version of a page on a mobile device by setting the cookie 
'force_device' - http://pastie.org/4445761
-Add a context processor that defines which base template to use based on 
request.is_mobile - http://pastie.org/4445772
-Chances are your templates currently extend base.html or something. 
 Change all those to {% extends BASE_TEMPLATE %}

Now all your templates will extend either base.html or base_mobile.html 
based on the user agent.  You can tweak the layout for the mobile site by 
doing {% if request.is_mobile %}...{% else %}...{% endif %}, or if you are 
going to have drastically different content if the user is on mobile, you 
can define two content blocks in your test.html template, "content" and 
"content-mobile", and then have base.html and base_mobile.html render each 
of those blocks.

-Matt

On Thursday, August 9, 2012 9:29:46 AM UTC-4, MrMuffin wrote:
>
> Hmmm ... is there any other way to achieve the same result ( using a 
> special template for mobile devices ) without hacking this into each 
> view? MiddleWare using 
>
>
> https://docs.djangoproject.com/en/dev/topics/http/middleware/?from=olddocs/#process-template-response
>  
>
> for instance? 
>
> Anyway, thanks for your input. 
>
> Thomas 
>
> On Thu, Aug 9, 2012 at 3:09 PM, Daniel Roseman 
> <dan...@roseman.org.uk<javascript:>> 
> wrote: 
> >> On Thursday, 9 August 2012 13:18:36 UTC+1, MrMuffin wrote: 
> >>> 
> >>> I need to change what template to use in a response based on type of 
> >>> user-agent in request. The middleware below works ok to detect mobile 
> >>> client, but I cannot get the template manipulation in the process_view 
> >>> method  to work. 
> >>> 
> >>> The middleware: 
> >>> 
> >>> # Credits: http://djangosnippets.org/snippets/2001/ 
> >>> import re 
> >>> 
> >>> 
> >>> class MobileDetectionMiddleware(object): 
> >>>     """ 
> >>>     Useful middleware to detect if the user is 
> >>>     on a mobile device. 
> >>>     """ 
> >>> 
> >>>     def process_view(self, request, view_func, view_args, 
> view_kwargs): 
> >>>         if not request.is_mobile: 
> >>>             return 
> >>> 
> >>>         print vars(view_func), view_args,view_kwargs # these are 
> >>> allways blank/empty 
> >>>         template = view_kwargs.get("template") 
> >>>         if template is None and view_func.func_defaults: 
> >>>             for default in view_func.func_defaults: 
> >>>                 if str(default).endswith(".html"): 
> >>>                     template = default 
> >>>                     break 
> >>> 
> >>>         if template is not None: 
> >>>             template = template.rsplit(".html", 1)[0] + ".mobile.html" 
> >>>             try: 
> >>>                 get_template(template) 
> >>>             except TemplateDoesNotExist: 
> >>>                 return 
> >>>             else: 
> >>>                 view_kwargs["template"] = template 
> >>> 
> >>>         return view_func(request, *view_args, **view_kwargs) 
> >>> 
> >>> <snip> 
> >>> 
> >>> 
> >>> from django.shortcuts import * 
> >>> 
> >>> def index(request): 
> >>>     return render_to_response('testapp/test.html', 
> {'user':request.user}) 
> >>> 
> >>> 
> >>> 
> >>> Any clues? 
> >>> 
> >>> 
> >>> -- 
> >>> Mvh/Best regards, 
> >>> Thomas Weholt 
> >>> http://www.weholt.org 
> >> 
> >> 
> > 
> > I can't imagine how you are expecting this to work. The template in your 
> > view is not a parameter, but is hard-coded into the call to 
> > render_to_response. Your code appears to be wanting to access the 
> default 
> > value of a non-existent view parameter, modify it, and pass it back into 
> a 
> > view that isn't expecting it. 
> > 
> > It seems like you want to make `template` a parameter to the view, and 
> then 
> > your middleware could modify that. 
> > -- 
> > DR. 
>
>
>
> -- 
> Mvh/Best regards, 
> Thomas Weholt 
> http://www.weholt.org 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ohSlUh3_h3sJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to