Wraithan
Wraithan

Reputation: 1257

Django index page best/most common practice

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.

Upvotes: 21

Views: 12770

Answers (2)

TM.
TM.

Reputation: 111077

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

Upvotes: 19

ayaz
ayaz

Reputation: 10502

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

Upvotes: 3

Related Questions