如何将数据传递到django中的视图? [英] How do I pass data to a view in django?

查看:197
本文介绍了如何将数据传递到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[^]


这篇关于如何将数据传递到django中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆