CC Experiment 9
CC Experiment 9
Aim: Develop a hello world program web application and deploy it on the Google app
engine.
Description:
We are preparing a web application for simple “Hello world” Application and host it on
Google App Engine.
Django is a MVC architecture to develop a web application. MVC means Model – View –
Controller. This is a popular way to internally separate the data, logic, and display of an
application into separate components that are easier for a developer to reason about.
Steps 1: Create a directory for storing project and its components using following commands
# Windows
$ cd onedrive\desktop\code
$ mkdir helloworld
$ cd helloworld
Step 2: Install the Django package on python using following command Run the following
commands
To add a new app go to the command line and quit the running server with Control+c. Then
use the startapp command followed by the name of our app which will be pages.
1. Add “pages” at bottom in settings.py file under INSTALLED_APPS tag to let know
Django that application is created.
Even though our new app exists within the Django project, Django doesn’t “know”
about it until we explicitly add it to the django_project/settings.py file.
# django_project/settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"pages", # new
]
view is a Python function that accepts a Web request and returns a Web response. The
response can be the HTML contents of a Web page, a redirect, a 404 error, an image, or
really anything.
def homepage(request):
return HttpResponse("Hello World!")
3. Moving along we need to configure our URLs. In your text editor, create a new file
called urls.py within the pages app. Then update it with the following code:
# pages/urls.py
from django.urls import path
urlpatterns = [
path("", homepage, name="home"),
]
4. We’re almost done at this point. The last step is to update
our django_project/urls.py file. It’s common to have multiple apps within a single
Django project, like pages here, and they each need their own dedicated URL path.
# django_project/urls.py
from django.contrib import admin
from django.urls import path, include # new
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("pages.urls")), # new
]
We’ve imported include on the second line next to path and then created a new URL
pattern for our pages app. Now whenever a user visits the homepage, they will first be
routed to the pages app and then to the homePageView view set in
the pages/urls.py file.
This need for two separate urls.py files is often confusing to beginners. Think of the
top-level django_project/urls.py as the gateway to various URL patterns distinct to
each app.
We have all the code we need now. To confirm everything works as expected, restart
our Django server:
If you refresh the browser for http://127.0.0.1:8000/ it now displays the text “Hello,
World!”