python Django 索引页最佳/最常见的做法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1940528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:24:09  来源:igfitidea点击:

Django index page best/most common practice

pythondjangoindexing

提问by Wraithan

I am working on a site currently (first one solo) and went to go make an index page. I have been attempting to follow django best practices as I go, so naturally I go search for this but couldn't a real standard in regards to this.

我目前正在一个网站上工作(第一个独奏)并去制作索引页面。我一直在尝试遵循 django 最佳实践,所以我很自然地去寻找这个,但在这方面没有一个真正的标准。

I have seen folks creating apps to serve this purpose named various things (main, home, misc) and have seen a views.py in the root of the project. I am really just looking for what the majority out there do for this.

我见过人们创建应用程序来满足此目的,命名为各种事物(main、home、misc),并且在项目的根目录中看到了一个 views.py。我真的只是在寻找大多数人为此所做的事情。

The index page is not static, since I want to detect if the user is logged in and such.

索引页面不是静态的,因为我想检测用户是否登录等。

Thanks.

谢谢。

采纳答案by TM.

If all of your dynamic content is handled in the template (for example, if it's just simple checking if a user is present on the request), then I recommend using a generic view, specificially the direct to templateview:

如果您的所有动态内容都在模板中处理(例如,如果只是简单地检查请求中是否存在用户),那么我建议使用通用视图,特别是直接到模板视图:

urlpatterns = patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'index.html'}),
)

If you want to add a few more bits of information to the template context, there is another argument, extra_context, that you can pass to the generic view to include it:

如果您想向模板上下文添加更多信息,还有另一个参数 ,extra_context您可以将其传递给通用视图以包含它:

extra_context = { 
    'foo': 'bar',
    # etc
}
urlpatterns = patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'index.html', 'extra_context': extra_context }),
)

回答by ayaz

I tend to create a views.pyin the root of the project where I keep the indexview.

我倾向于views.py在我保留index视图的项目的根目录中创建一个。