Django索引页面最好/最常见的做法

时间:2021-09-13 21:14:48

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.

谢谢。

2 个解决方案

#1


19  

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'}),
)

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 }),
)

#2


3  

I tend to create a views.py in the root of the project where I keep the index view.

我倾向于在项目的根目录中创建一个views.py来保存索引视图。

#1


19  

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'}),
)

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 }),
)

#2


3  

I tend to create a views.py in the root of the project where I keep the index view.

我倾向于在项目的根目录中创建一个views.py来保存索引视图。