简体   繁体   English

Django中的动态模板

[英]Dynamic templates in Django

I'm about to make a dynamic website in Django so that user can change the template the website is based upon....for new users, they'll have to choose a template they like from a menu, after which this is stored in a cookie for future reference. 我即将在Django中创建一个动态网站,以便用户可以更改网站所基于的模板....对于新用户,他们必须从菜单中选择他们喜欢的模板,然后存储在cookie中供将来参考。 I however don't know how to do this....any ideas. 但是我不知道怎么做......任何想法。

Templates are text files in a directory on your server. 模板是服务器上目录中的文本文件。

The menu is a list of directories. 菜单是目录列表。

The specific template to be loaded is named in your render_to_response calls in each view function. 要加载的特定模板在每个视图函数的render_to_response调用中命名。

It's wonderfully quite simple and elegant. 它非常简单而优雅。

You never need to do a template "swap". 你永远不需要做模板“交换”。 You can simply have all the templates you ever want in a template search path. 您可以在模板搜索路径中获得所需的所有模板。 All can be available to all users at all times. 所有用户都可以随时使用。 You just provide directory_name/template_name . 您只需提供directory_name/template_name All handled for you. 全部为您处理。

Don't mess with explicit cookies. 不要搞乱明确的cookie。 Django already does this for you. Django已经为你做了这个。 See chapter 12 of the Django Book. 请参阅Django Book的第12章

Use the Profiles extension and put the selected template directory name in the user's profile. 使用“ 个人档案”扩展 ,并将选定的模板目录名称放在用户的个人档案中。

When user selects a template, store the template name/ some sort of alias in the cookie. 当用户选择模板时,将模板名称/某种别名存储在cookie中。 When the user loads the page again, in the view function, check for template identifier in the cookie. 当用户再次加载页面时,在视图功能中,检查cookie中的模板标识符。 If the identifier is present, retrieve the actual template path and pass it to the render_to_response function. 如果存在标识符,则检索实际模板路径并将其传递给render_to_response函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM