Detect mobile browser (not just iPhone) in python view

后端 未结 3 826
说谎
说谎 2020-12-23 17:04

I have a web application written in Django that has one specific page I\'d like to implement a mobile version of the template (and slightly different logic) for. I\'d like t

3条回答
  •  星月不相逢
    2020-12-23 17:52

    best practice: use minidetector to add the extra info to the request, then use django's built in request context to pass it to your templates like so.

    from django.shortcuts import render_to_response
    from django.template import RequestContext
    
    def my_view_on_mobile_and_desktop(request)
        .....
        render_to_response('regular_template.html', 
                           {'my vars to template':vars}, 
                           context_instance=RequestContext(request))
    

    then in your template you are able to introduce stuff like:

    
      
      {% block head %}
        blah
      {% if request.mobile %}
        
      {% else %}
        
      {% endif %}
      
      
        
        {% if not request.mobile %}
        
        {% endif %>
        
    {% if not request.mobile %} {% endif %}

    article content

提交回复
热议问题