Create Home Page for Django App

Home page for Django app


Open the settings file and add the app to the list of installed apps.

 

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'<appname>',
'django.contrib.admin',
)

Open the main urls.py file under the <project>/urls.py and add the url configuration.

 

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^admin/', include(admin.site.urls)),
)

If a user navigates to our home page, Django should look for the index function located in the home view. In order for Django to know that the view exists, add the following at the top of your urls.py file

 

from <app> import views

Under the views.py file under the <app> folder – Add the following

 

from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
return render_to_response('<app>/index.html', context_instance=RequestContext(request))

Now make a template for home page (index)

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>My Home Page</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css" media="screen" />

</head>
<body>
<p>My Content</p>
</body>
</html>