问题
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