基于 Django class 的视图移动模板
Django class based view mobile template
我正在设置 Django 以使用不同的移动模板,但我不确定如何为基于 Class 的视图设置它。如果我将它包含在 class 中,如下所示,它会引发错误。
class EventList(ListView):
model = Event
paginate_by = 10
context_object_name = 'events'
category = None
area = None
starts = None
ends = None
slug_level = ""
if request.mobile:
template_name = "mobile/mobile.html"
...
我有类似 def get_queryset(self):
的功能,所以它使用不同的移动模板,因为请求不在基于 class 的视图中
我在函数中使用 minidetector
,如下所示:
@detect_mobile
def home(request, template_name='pages/home.html'):
....
if request.mobile:
return render_to_response('mobile/mobile.html', context)
移动端和桌面端通常使用相同的 HTTP 方法,因此 CBV 不是您区分这些客户端的地方。
相反,您应该查看客户端的属性,例如 user-agent header 并相应地设置模板名称。
我认为以下页面提供了很好的介绍:
我正在设置 Django 以使用不同的移动模板,但我不确定如何为基于 Class 的视图设置它。如果我将它包含在 class 中,如下所示,它会引发错误。
class EventList(ListView):
model = Event
paginate_by = 10
context_object_name = 'events'
category = None
area = None
starts = None
ends = None
slug_level = ""
if request.mobile:
template_name = "mobile/mobile.html"
...
我有类似 def get_queryset(self):
的功能,所以它使用不同的移动模板,因为请求不在基于 class 的视图中
我在函数中使用 minidetector
,如下所示:
@detect_mobile
def home(request, template_name='pages/home.html'):
....
if request.mobile:
return render_to_response('mobile/mobile.html', context)
移动端和桌面端通常使用相同的 HTTP 方法,因此 CBV 不是您区分这些客户端的地方。
相反,您应该查看客户端的属性,例如 user-agent header 并相应地设置模板名称。
我认为以下页面提供了很好的介绍: