如何将数据传递到django中的视图?

问题描述:

我想为不同的设备显示不同的页面,例如PC和移动设备。有没有办法告诉用户代理请求,并将结果(设备类型)传递给相应的视图?我不想在每个视图中区分它们。

我能找到的是View类的子类并覆盖dispatch方法。告诉dispath方法中访问设备的类型并将结果保存为类的属性,所以在get和post方法中我可以通过self.result得到它。

I want to show different page for different device, such as PC and mobile. Is there any way to tell the user-agent in request, and pass the result(the type of device) to the corresponding view? I don't want to differentiate between them in each view.
what i can find is subclass the View class and override the dispatch method. tell the type of access device in the dispath method and save the result as a attribute of the class, so in get and post method i can get it by self.result.

最好也是最简单的方法是使用会话。

https://docs.djangoproject.com/en/dev/topics/http/sessions/#topics-http-sessions [ ^ ]
The best and also the easiest way is to use sessions.
https://docs.djangoproject.com/en/dev/topics/http/sessions/#topics-http-sessions[^]