Change Django Templates Based on User-Agent

前端 未结 10 2541
耶瑟儿~
耶瑟儿~ 2020-12-07 11:37

I\'ve made a Django site, but I\'ve drank the Koolaid and I want to make an IPhone version. After putting much thought into I\'ve come up with two options:

10条回答
  •  生来不讨喜
    2020-12-07 12:01

    best possible scenario: 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

提交回复
热议问题