我正在尝试用django开发一个移动和pc浏览器的网站。我正在尝试找出视图和模板的最佳结构。这是我尝试过的:
1)使用不同的url (如http://example.com/mobile/和http://example.com/或http://example.com/?c=mobile )来区分手机和pc,并将它们映射到不同的视图,设置不同的模板。
2)在视图中根据USER_CLIENT设置不同的模板
3)使用视图的包装层,实际视图只是将数据返回给包装器,包装器设置不同的模板。
在django中有没有一种通用的方法来处理这个问题?有什么建议和意见吗?
发布于 2010-02-09 21:05:03
在http://m.example.com上使用Django的"sites" framework作为手机版本。
发布于 2010-03-24 04:55:25
我推荐解决方案3;使用装饰器检查客户端用户代理,并在移动代理的情况下返回不同的模板。
让装饰器接受两个参数:普通模板和移动模板。
从您的视图中,返回装饰器可以作为上下文传递给呈现函数的字典。有一个叫做'render_to‘的装饰器在这方面做得很好,谷歌就是这样的。
为了处理用户想要完整版本的用例,甚至在从移动设备浏览时,您可以使用重定向视图来设置您的装饰者可能会检查的cookie。
发布于 2010-11-11 15:52:10
最佳实践:使用minidetector将额外的信息添加到请求中,然后使用django的内置请求上下文将其传递给您的模板,如下所示。
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))然后,在您的模板中,您可以引入如下内容:
<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>https://stackoverflow.com/questions/2220251
复制相似问题