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 manage.py
cd C:\Users\john.knight\OneDrive\Django\mysite
python manage.py startapp polls
This creates the poll directory with the following content:
admin.py
apps.py
\migrations
__init__.py
models.py
test.py
views.py
__init__.py
2) Create the first view
Open polls\views.py 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 urls.py in the polls directory.
With an editor, add the following code to urls.py:
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 urls.py 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’)),
  url(r’^admin/’, admin.site.urls),
]
5) Test it works
Navigate to the mysite container directory
cd C:\Users\john.knight\OneDrive\Django\mysite
python manage.py runserver
Browse to http://127.0.0.1:8000/polls
You should now see the message “Hello, world. You’re at the polls index.” displayed in the brwoser.


Posted

in

by

Tags:

Comments

Leave a Reply

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