2 How to Make a Home Page
If you have the development server running, close it by doing
Now you can create a new Django app:
django-admin startapp accounts
It is called accounts here because the longer term goal for this series will be to create an entire login authentication and registration system.
In your project you now need to “install” the Django app you just created. Go to the
tutorial folder that you created in your project’s
tutorial/settings.py file add another element in the
So now your entire
INSTALLED_APPS list should look like this:
INSTALLED_APPS = [ 'accounts', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
Now that the new
accounts app has been installed, we also need to tell Django how we’re going to get to the new accounts app.
In the main url configuration, point Django to the accounts app when the user goes to (in this case)
/account. Go to
tutorial/urls.py and add a line to the
urlpatterns (which was defined on the initial creation of our Django project):
We also need to import
include so that line 16 now reads:
from django.conf.urls import url, include
Putting that all together we should now have in
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^account/', include('accounts.urls')), ]
'accounts.urls' here means that when someone makes a request to
/account that Django will point them to the
urls/py file in the
accounts app (that’s the one that we installed earlier).
You may have noticed that that file doesn’t exist yet so let’s create a new file in `accounts/urls.py:
from django.contrib.urls import url from . import views urlpatterns = [ url(r'^$', views.home), ]
This tells Django to go to the not yet defined
home view function in search of a HTTP response of some kind to be able to send back to the user (or client) that made the initial request. Let’s define that in accounts/views.py:
from django.shortcuts import render, HttpResponse # Create your views here def home(request): return HttpResponse('Home page!')
Now you should be able to go to localhost:8000/account to see your brand new, Django powered home page!