views:

512

answers:

2

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.

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.

Thanks.

+6  A: 

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 = { 
    'foo': 'bar',
    # etc
}
urlpatterns = patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'index.html', 'extra_context': extra_context }),
)
TM
I ended up using this method. After conversing with my local django user group they also agreed this was the best method.
Chris McDonald
+2  A: 

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

ayaz
From what others have said, it seems that this is the second best option, if you need a full view instead of just direct_to_template.
Chris McDonald