Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 301
Wraithan Avatar asked Dec 21 '09 14:12

Wraithan


2 Answers

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 }),
)
like image 65
TM. Avatar answered Sep 23 '22 17:09

TM.


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

like image 22
ayaz Avatar answered Sep 25 '22 17:09

ayaz