Creating the “polls” Django app

From the online documentation…
Projects vs Apps: An app is a web application that does something, a project is a collection of configuration and apps. A project can contain multiple apps and an app can be in multiple projects.
Apps can live anywhere on the Python path.
Creating the polls app
1) Create the app
navigate to same direcotry as
cd C:\Users\john.knight\OneDrive\Django\mysite
python startapp polls
This creates the poll directory with the following content:
2) Create the first view
Open polls\ in an editor and enter the following code so that the file looks like:
from django.http import HttpResponse
def index(request):
return HttpResponse(“Hello, world. This is the polls index.”)
To call this view, it needs to be mapped to a URL by using a URLconf.
3) Create the URLconf
Create a file called in the polls directory.
With an editor, add the following code to
from django.conf.urls import url
from . import views
urlpatterns = [
  url(r’^$’, views.index, name=’index’),
4) Point URLconf at rool level to the polls.urls module.
Navigate to the mysites directory.
cd C:\Users\john.knight\OneDrive\Django\mysite\mysite
Amend the existing contents of so that it looks like this:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
  url(r’^polls/’, include(‘polls.urls’)),
5) Test it works
Navigate to the mysite container directory
cd C:\Users\john.knight\OneDrive\Django\mysite
python runserver
Browse to
You should now see the message “Hello, world. You’re at the polls index.” displayed in the brwoser.






Leave a Reply

Your email address will not be published. Required fields are marked *