Django索引页面最佳/最常见的做法 [英] Django index page best/most common practice

查看:85
本文介绍了Django索引页面最佳/最常见的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上工作(第一个独奏),然后去做一个索引页。我一直试图遵循django最佳做法,所以我自然会去寻找这个,但不能成为一个真正的标准。

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.

我已经看到为了这个目的而创建应用程序的人们将其命名为各种各样的东西(主,家,杂),并在项目的根目录中看到一个views.py。我真的只是在寻找这个大多数人。

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.

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

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

谢谢。

推荐答案

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

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 template view:

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

如果你想添加一些信息,模板上下文中还有另一个参数 extra_context ,可以传递给通用视图以包含它:

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 = { 
    'foo': 'bar',
    # etc
}
urlpatterns = patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'index.html', 'extra_context': extra_context }),
)

这篇关于Django索引页面最佳/最常见的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆