开发者

build website for mobile and pc with django

I am trying to develop a website for mobile and pc browser with django. and I am trying to figure out a best structure of the views and templates. there is what I have tried:

1) use different url ( like http://example.com/mobile/ and http://example.com/ OR http://example.com/开发者_开发知识库?c=mobile ) to distinguish mobile and pc, and map them to different view which set different templates.

2) in the view set different template according to USER_CLIENT

3) use a wrapper layer of the view, the actual view just return the data to the wrapper, the wrapper set the different template.

Is there a common way to handle this in django? any suggestions and comments?


Use Django's "sites" framework for a mobile version at http://m.example.com.


I would recommended solution 3; using a decorator to inspect the clients User Agent and returning à different template in case of a mobile agent.

Have the decorator take two arguments: the normal template, and the mobile template.

From your view, return a dict The decorator may pass to the rendering function as context. There is a decorator called 'render_to' that does this very well, Google for it.

To deal with the use case where users want the full version, even when browsing from a mobile device, you may use a redirecting view that sets a cookie your decorator may check for.


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:

<html>
  <head>
  {% block head %}
    <title>blah</title>
  {% if request.mobile %}
    <link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-mobile.css">
  {% else %}
    <link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-desktop.css">
  {% endif %}
  </head>
  <body>
    <div id="navigation">
      {% include "_navigation.html" %}
    </div>
    {% if not request.mobile %}
    <div id="sidebar">
      <p> sidebar content not fit for mobile </p>
    </div>
    {% endif %>
    <div id="content">
      <article>
        {% if not request.mobile %}
        <aside>
          <p> aside content </p>
        </aside>
        {% endif %}
        <p> article content </p>
      </aricle>
    </div>
  </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜