使用 Django 平面页面时如何获取父页面?
由于 Django url 是任意的,包括创建平面页面时设置的那些。 如何知道页面的父页面是什么? 也就是说,如果我创建一个页面 /about/contact/,那么当我位于联系页面(在模板中)时,我如何找出父页面是什么?
有没有标准的方法来做到这一点? 或者我只是将斜线分开并使用第一部分?
基本上我想弄清楚如何创建不同的网站部分。
Since Django urls are arbitrary, including those set when you create flatpages. How can you figure out what a page's parent is? That is if I create a page /about/contact/, then when I am on the contact page (in the template), how can I figure out what the parent page is?
Is there a standard way to do this? Or do I just split the slug on the slashes and use the first section?
Basically I'm trying to figure out how to create different site sections.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,django 没有本地的节概念,并且正如您所说,URL 是任意的(它们留给程序员决定)。 如果您在整个网站(/
我宁愿在 url 模式中设置该部分并将其传递给视图(如 here) 这又可以将其传递给模板...
As far as I know, django doesn't have a native concept of sections and, as you said, the URLs are arbitrary (they're left to the programmer to decide). If you stick with the same URL structure throughout your whole site (/<section>/page) then you can infer the section from the URL.
I'd rather set the section in the url patterns and pass it to the views (as described in here) which in turn can pass it to the templates...
平面页面本质上与静态 html 页面相同,存储在数据库中,前面有一个漂亮的管理员。
所以只需硬编码一个链接,这应该不是什么大问题。
如果这是一个大问题,那就意味着 Flatpages 可能不是您的解决方案
flatpages are essentially the same thing as static html pages, stored in your database with a pretty admin in front of it.
so just hard code a link, it shouldn't be a big deal.
if it is a big deal, that means flatpages probably isn't the solution for you