gpt4 book ai didi

python - 基于 Django 类的 View 移动模板

转载 作者:行者123 更新时间:2023-11-28 18:38:04 24 4
gpt4 key购买 nike

我正在设置 Django 以使用不同的移动模板,但我不确定如何为基于类的 View 设置它。如果我将它包含在下面的类中,它会抛出错误。

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): 这样的函数,把它放在哪里,因为请求不在基于类的 View 中,所以它使用不同的移动模板

我在函数中使用 minidetector 是这样的:

@detect_mobile
def home(request, template_name='pages/home.html'):
....
if request.mobile:
return render_to_response('mobile/mobile.html', context)

最佳答案

移动端和桌面端通常使用相同的 HTTP 方法,因此 CBV 不是您区分这些客户端的地方。

相反,您应该查看客户端的属性(例如用户代理 header )并相应地设置模板名称。

我认为以下页面提供了很好的介绍:

http://mobiforge.com/design-development/build-a-mobile-and-desktop-friendly-application-django-15-minutes

关于python - 基于 Django 类的 View 移动模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com