Get screen size with Django not using JavaScript

雨燕双飞 提交于 2020-01-04 06:59:07

问题


How do I get the current screen size and store it in session using Django? I would like to use different template code depending on screen size, whether it's a full laptop/desktop size, or mobile, etc.

I'm trying to avoid using JavaScript to cater to mobile phones that do not have Javascript on their browsers.

Thank you!


回答1:


window.screen.height; //screen height
window.screen.width; //screen width

//For example, 1024x768:
alert(window.screen.width+"x"+window.screen.height);

You can then store that using either localStorage or do an AJAX request to store it in a session.




回答2:


You want to get and probably parse the User-Agent header from the request. Many devices will send their screen resolution in that. There's a few other ideas on this web page:

http://johannburkard.de/blog/www/mobile/mobile-phone-pda-web-browser-screen-size-detection.html

since it seems some web clients have their own headers for this.




回答3:


Thanks for your answers! I eventually used minidetector. Seems to be working fine.



来源:https://stackoverflow.com/questions/5779570/get-screen-size-with-django-not-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!